arduino 程序解析, 求真正理解单片机的牛人

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了arduino 程序解析, 求真正理解单片机的牛人相关的知识,希望对你有一定的参考价值。

#include <SoftPWM.h>
#include <SoftPWM_timer.h>
#include <TimerOne.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include "pins_arduino.h"
volatile unsigned char count72kHz = 0;
volatile boolean toggle;
void setup()
Serial.begin(9600);

pinMode(8, OUTPUT); //led2
pinMode(11, OUTPUT); //ultrasonic signal out
pinMode(3, OUTPUT); //right motor backward
pinMode(10, OUTPUT); //left motor backward
pinMode(5, OUTPUT); //right motor foreward
pinMode(9, OUTPUT); //left motor foreward

SoftPWMBegin();
SoftPWMSet(3,0);
SoftPWMSet(5,0);
SoftPWMSet(9,0);
SoftPWMSet(10,0);

/* TCCR2A = (1 << WGM21) | (1 << COM2A0);
TCCR2B = (1 << CS20);
OCR2A = 0xDC; // 36kHz @16MHz
TIMSK2 |= (1 << OCIE2B); // 36kHz counter for sleep

sei();//enable interrupts*/
interrupts();
Timer1.initialize(65536);
Timer1.attachInterrupt(timer_isr);
toggle=true;
Serial.write("Hello AAR\n");

void timer_isr()
//ISR(TIMER2_COMPB_vect) // Interrupt service routine to output next sample to PWM
count72kHz ++;
if(toggle)
digitalWrite(11, !digitalRead(11));


void loop()
int pos, i;
int posmarker;
LocalInit();
while(true)
posmarker = 0;
Ping(10); //send burst with ultrasonic signal
for(pos = 0; pos < 100; pos++)
delay(10);
if((ACSR & (1 << ACI)) != 0) //check if the compare interrupt flag is set
if(posmarker == 0)
posmarker = pos;


ACSR |= (1 << ACI);

Serial.write(posmarker);
if(posmarker > 10)
digitalWrite(8,HIGH);
SoftPWMSet(3,200);
SoftPWMSet(5,0);
SoftPWMSet(9,0);
SoftPWMSet(10,200);

else
digitalWrite(8, LOW);
SoftPWMSet(3,0);
SoftPWMSet(5,200);
SoftPWMSet(9,200);
SoftPWMSet(10,0);
//delay(200);

PingStop();//turn off the ultrasonic signal


void LocalInit(void)

// Change Oscillator-frequency of Timer 2
// to 40kHz, no toggling of IO-pin:
/*TCCR2A = _BV(WGM21);
TCCR2B = _BV(CS20);
OCR2A = 0xC7; // 40kHz @16MHz crystal*/
Timer1.initialize(62944);
toggle=false;
ADCSRA = 0x00; // ADC off
// Analog comparator:
ACSR = 0x02; // Generate interrupt on falling edge
ADMUX = 0x05; // Multiplexer for comparator to
// ADC pin 3
ADCSRB |= _BV(ACME); // Enable muliplexing of comparator
pinMode(6, INPUT); // Port D Pin 6 is input!


void Ping(unsigned char length)

count72kHz = 0;
/* TCCR2A = _BV(WGM21) | _BV(COM2A0);
TCCR2B = _BV(CS20);*/
// Toggling of IO-Pin on
toggle=true;
// generate the Chirp
while(count72kHz < length)
Timer1.initialize((0xC7 + length / 2 - count72kHz)*256);


void PingStop()
/* TCCR2A = _BV(WGM21);
TCCR2B = _BV(CS20); // Toggling of IO-Pin off*/
toggle = false;
Timer1.initialize(0xDC*256); // set frequency to 36kHz @16MHz

ATmega328P

有以下疑问:
1. SoftPWMSet 这是什么鬼? arduino的pwm输出,可以直接使用analogwrite函数。若是你自己写的,;源文件丢出来给人看看是啥。
2. 还有"pins_arduino.h"也是,如果是自己写的,都把它贴出来。
3. arduino的中断,不是这样用的。直接使用<avr/interrupt.h>,会造成冲突.
4. 对你这个程序,你遇到的困难是?想跟网友求助那个问题?看了10多分钟,还是一头雾水。你这个我大概看了下,目测你的程序是这样的:3,5,10,9,这四个引脚来控制两个直流电机,直接扩流来驱动电机,引脚8接的是一个LeD,用于显示状态,还有个11引脚,从注释的字面上理解,应该是超声波的相关引脚,但是一个引脚,超声波,我愚钝,看不出来的是干嘛。
看了10多分钟,还没搞懂你想要干嘛,根据超声波测得的距离,来控制小车前进后退?还是前进过程中,超声波检测到障碍来做避障?
根据前面的分析,你这段代码,肯定是不通过编译的,错误提示呢?
你要网友帮你修正程序,但是又不知道你的需求,怎么改?
总之,分给我吧~~~追问

如图,其实是想知道图片中pin脚OC2和PD6两个到底怎样驱动超声波传感器的发射以及如何通过ADC2进行中断,大致解释一下就行了。分是一定给你了。

追答

你这图不是很清晰,看不出哪个是PD6,哪个是OC2。
看你这个,应该不是超声波模块。
超声波我也没搞过,我也大概猜测一下控制过程,仅供参考:
超声波的发射,其实就跟普通蜂鸣器一样,只是蜂鸣器能响应的频率比较宽而已,而这超声波发射模块,能响应的部分,比高音喇叭还要高的频率,通常是40KHz。你这不是模块,需要arduino来发送方波,控制相当于无源蜂鸣器。也就是你图中下面一个你扣了圈圈的IO口,给5个(8个10个也可以,随便喜欢)40Khz的方波,超声波就发送出去了。
超声波的接收,其实就是相当于一个话筒,经过一个运放,直接把信号给回了arduino。上面发送方波后,就启动一个计时,然后就判断这输入脚(也就是你图中上面一个你扣了圈圈的IO口),是不是收到了5个(8个10个)方波,是的话,就是超声波返回了,结束计时。计时得到的时间,就是声音来回走所花费的时间。

追问

问题在于小车已经制定好,最后单片机器受到的就是一个模拟信号,如何通过这个接受到的模拟信号判断小车距离.

参考技术A 太复杂了 不会有人给你解释的

就日常记录一下我所知道打一些牛人吧

工业界

陈天奇([email protected][email protected][email protected]

https://github.com/tqchen

sjtu 致远出来的牛人,xgboost,mxnet,tvm这些其中一个就够一个码农吹一辈子了吧。。。

吴育昕([email protected]_cs/ [email protected]

http://ppwwyyxx.com/blog/

看这个大牛的博客和github,真心服。博客写得数学类专栏很专业,github更是一片"绿"。

张弛原(外号pluskid, [email protected]/[email protected]/[email protected]

http://freemind.pluskid.org/

zju出来的ai巨佬,是从xiaofeihe和deng cai,现在在google brain

当然还少不了一些比较低调的大佬,如kaiming he(resnet作者) , yangqing jia(caffe作者), mu li(mxnet)等等

 

 

学术界:

cv: kaiming he,xiaoou tang, xiaogang wang, xiaofei he, deng cai, feifei li

nlp: danqi chen, zhiyuan liu

shuicheng yan, zhihua zhou ,  qi lu, 

 

 

 

 

 

以上观点都是个人看法,仅供参考。

 

以上是关于arduino 程序解析, 求真正理解单片机的牛人的主要内容,如果未能解决你的问题,请参考以下文章

水平集——那些我膜拜过的牛人2

史上最全,国内自然语言处理的牛人团队大赏

听说 新东方 的健身房有游泳池,听说 小米 的牛人非常多

牛人都是如何使用emacs的

拿到字节抖音拼多多美团三个offer的牛人,分享自己的面经和心得体会!

如何烧录 arduino 的 bootloader?