|
|||||||||||
| 技術(shù)交流 | 電路欣賞 | 工控天地 | 數(shù)字廣電 | 通信技術(shù) | 電源技術(shù) | 測(cè)控之家 | EMC技術(shù) | ARM技術(shù) | EDA技術(shù) | PCB技術(shù) | 嵌入式系統(tǒng) 驅(qū)動(dòng)編程 | 集成電路 | 器件替換 | 模擬技術(shù) | 新手園地 | 單 片 機(jī) | DSP技術(shù) | MCU技術(shù) | IC 設(shè)計(jì) | IC 產(chǎn)業(yè) | CAN-bus/DeviceNe |
m168芯,PWM調(diào)制問題,非常奇怪!有好奇心得朋友進(jìn)來看看 |
| 作者:PARK1358 欄目:單片機(jī) |
本想用PWM細(xì)分驅(qū)動(dòng)雙極性步進(jìn)電機(jī)的,有一個(gè)奇怪的問題導(dǎo)致始終不能成功. m168芯,設(shè)置為相位可調(diào)PWM模式,在定時(shí)器溢出時(shí)改變OCRA,OCRB的值,不斷改變比較寄存器OCRA / OCRB的值,但發(fā)現(xiàn)每次查數(shù)據(jù)表首時(shí),OCRA與OCRB比較一次 然后從OCA腳輸出一個(gè)比較值,導(dǎo)致整個(gè)信號(hào)的錯(cuò)誤.郁悶!!!! 以下是簡(jiǎn)化后的源代碼(功能只是按我需要的方式檢測(cè)PWM性能)及示波器觀測(cè)到的波形. //ICC-AVR application builder : 2006-3-18 21:25:04 // Target : M168 // CRYSTAL: 8.0000Mhz #include <iom168v.h> #include <macros.h> unsigned CHAR MOTORDATA_A[7] = { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, }; unsigned CHAR MOTORDATA_B[7] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00, } ; unsigned CHAR MOTOROUTA = 0x00 ; unsigned CHAR MOTOROUTB = 0x00 unsigned CHAR OUTSIGN = 0 ; void port_init(void) { PORTD = 0x60; DDRD = 0x60; } //-----------定時(shí)器0初始化---------------------------- void timer0_init(void) { TCCR0B = 0x00; //stop TCNT0 = 0x00 /*INVALID SETTING*/; //set count OCR0A = 0x00 ; OCR0B = 0x00 ; TCCR0A = 0xA1; //設(shè)置相位調(diào)整PWM模式,OC0A,OC0B端口升序匹配時(shí)置0,降序匹配時(shí)置1 TCCR0B = 0x05; //start timer 設(shè)置1024分頻(讓信號(hào)變化顯著一些,所以設(shè)這么大) } //--------------定時(shí)器中0斷處理------------------------ #pragma interrupt_handler timer0_ovf_isr:17 void timer0_ovf_isr(void) { OCR0A = MOTOROUTA ; //改變比較寄存器的值 OCR0B = MOTOROUTB ; //TIMER0 has overflowed STEP +=1 ; //數(shù)據(jù)表偏移量 OUTSIGN = 0xFF ; //數(shù)據(jù)輸出標(biāo)志設(shè)置 } \\------------查數(shù)據(jù)表-------------------------------- void MOTORDRIVE (void) { if (OUTSIGN == 0xFF) //數(shù)據(jù)輸出標(biāo)志判斷 { OUTSIGN = 0 ; if (STEP <= 7) { MOTOROUTA = MOTORDATA_A[STEP]; //查表更改數(shù)據(jù) MOTOROUTB = MOTORDATA_B[STEP]; } else STEP = 0 ; } } //--------開定時(shí)器0中斷--------------------------------- void init_devices(void) { CLI(); //disable all interrupts port_init(); timer0_init(); MCUCR = 0x00; EICRA = 0x00; //extended ext ints EIMSK = 0x00; TIMSK0 = 0x01; //timer 0 interrupt sources TIMSK1 = 0x00; //timer 1 interrupt sources TIMSK2 = 0x00; //timer 2 interrupt sources PCMSK0 = 0x00; //pin change mask 0 PCMSK1 = 0x00; //pin change mask 1 PCMSK2 = 0x00; //pin change mask 2 PCICR = 0x00; //pin change enable PRR = 0x00; //POWER controller SEI(); //re-enable interrupts //all peripherals are now initialized } //---------主程序--------------------------------------- void main(void) { init_devices(); while (1) MOTORDRIVE () ; //insert your functional code here... } |
|
|
| 免費(fèi)注冊(cè)為維庫(kù)電子開發(fā)網(wǎng)會(huì)員,參與電子工程師社區(qū)討論,點(diǎn)此進(jìn)入 |
Copyright © 1998-2006 m.58mhw.cn 浙ICP證030469號(hào) |