|
本帖最后由 glace12123 于 2020-9-9 11:49 编辑
pro mini或者uno板子,芯片是ATMega328P,小弟要输出周期可精确调整的脉冲,所以需要用到ISR函数来控制timer1,看网上大神写的程序,我仿照做了1个,在某个数字IO上输出方波,但可惜失败了。
我用delay()函数是没问题的,可惜delay函数精度不够。
所以求助大神,贴上源码,看看是否我的程序有问题,或者代码环境有问题,小弟先谢过。
- #define OUTPIN_led 13
- volatile boolean ledFlag = false;
- void Timer1Init()
- {
- TCCR1A = 0;
- TCNT1 = 0;
- TCCR1B = 0;
- TIMSK1 = (1<<OCIE1A);
- OCR1A = 62500-1; // count for 1s
- sei();
- }
- void Timer1Start()
- {
- TCCR1B = (1<<WGM12)|(1<<CS12)|(0<<CS11)|(0<<CS10); // 16MHz/256 = 625000 Hz
- }
- void setup()
- {
- // put your setup code here, to run once:
- pinMode(OUTPIN_led, OUTPUT);
- digitalWrite(OUTPIN_led, LOW);
- Timer1Init();
- }
- void loop()
- {
- // put your main code here, to run repeatedly:
- Timer1Start();
- }
- ISR(TIMER1_COMPA_vect)
- {
- digitalWrite(OUTPIN_led, ledFlag);
- ledFlag = ~ledFlag;
- }
复制代码
|
|