|
技術(shù)交流 | 電路欣賞 | 工控天地 | 數(shù)字廣電 | 通信技術(shù) | 電源技術(shù) | 測控之家 | EMC技術(shù) | ARM技術(shù) | EDA技術(shù) | PCB技術(shù) | 嵌入式系統(tǒng) 驅(qū)動編程 | 集成電路 | 器件替換 | 模擬技術(shù) | 新手園地 | 單 片 機(jī) | DSP技術(shù) | MCU技術(shù) | IC 設(shè)計 | IC 產(chǎn)業(yè) | CAN-bus/DeviceNe |
請教串口通訊程序 |
作者:ares_lan 欄目:單片機(jī) |
我的M8通過487與PC機(jī)的串口調(diào)試助手通訊.由串口調(diào)試助手發(fā)送FF控制板上的LED滅,發(fā)送DD控制LED亮!但是M8的發(fā)送實現(xiàn)不了。不知道是什么原因。程序如下: //ICC-AVR application builder : 2006-9-14 13:25:47 // Target : M8 // CRYSTAL: 8.0000Mhz #include <iom8v.h> #include <macros.h> #define uCHAR unsigned CHAR #define uint unsigned int #define LedOn PORTC &=~ (1<<PC0) #define LedOff PORTC |= (1<<PC0) #define DeSend PORTD |= (1<<PD4) //允許487發(fā)送 #define DeRece PORTD &=~(1<<PD4) //允許487接收 uCHAR TEMP; void port_init(void) { PORTB = 0x00; DDRB = 0x00; PORTC = 0x00; //M103 OUTPUT ONLY DDRC = 0x01; PORTD = 0x00; DDRD = 0x10; } //UART0 initialize // desired baud rate: 2400 // actual: baud rate:2404 (0.2%) // CHAR size: 8 bit // parity: Disabled void uart0_init(void) { UCSRB = 0x00; //disable while setting baud rate UCSRA = 0x00; UCSRC = BIT(URSEL) | 0x0E; UBRRL = 0xCF; //set baud rate lo UBRRH = 0x00; //set baud rate hi UCSRB = 0xD8; } #pragma interrupt_handler uart0_rx_isr:12 void uart0_rx_isr(void) { //uart has received a CHARacter in UDR TEMP = UDR; DeSend; } #pragma interrupt_handler uart0_tx_isr:14 void uart0_tx_isr(void) { //CHARacter has been transmitted UDR = TEMP; LedOff; // } //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts port_init(); uart0_init(); MCUCR = 0x00; GICR = 0x00; TIMSK = 0x00; //timer interrupt sources SEI(); //re-enable interrupts //all peripherals are now initialized } // void main(void) { init_devices(); //insert your functional code here... while(1) { DeRece; if(TEMP == 0xff) LedOff; if(TEMP == 0xdd) LedOn; } } 其中發(fā)送中斷程序進(jìn)入不了。其程序中的LedOff;沒有執(zhí)行。我想實現(xiàn)的是:從PC串口調(diào)試助手上發(fā)送什么,M8原樣反回什么! 小弟初做串口,還請各位多多指教! |
2樓: | >>參與討論 |
作者: 極限思考 于 2006/9/15 8:56:00 發(fā)布:
你發(fā)了嗎! |
3樓: | >>參與討論 |
作者: Sethhorus 于 2006/9/15 12:47:00 發(fā)布:
呵呵 沒發(fā)怎么會中斷 |
4樓: | >>參與討論 |
作者: zx1221 于 2006/9/15 17:58:00 發(fā)布:
我也想要這個功能,大家一起幫忙看看 也寫了個gcc的程序但是也不行 /************************************************ **** ATmega88 AT24C04 TWI,USART通信程序 *** **** 中斷方式UART 測試程序 *** **** *** **** 作者: Fantasy zx *** **** 編譯器:WinAVR-20050214-install *** **** *** ************************************************/ #include <avr/io.h> #include <avr/interrupt.h> #include <avr/signal.h> #include <avr/delay.h> #define uCHAR unsigned CHAR #define uint unsigned int volatile uCHAR get_bbfferTxdPos=0; //發(fā)送定位計數(shù)器 volatile uCHAR get_bbfferTxdLen=0; //等待發(fā)送字節(jié)數(shù) volatile uCHAR get_bbfferRxdPos=0; //接收定位計數(shù)器 volatile uCHAR get_bbfferRxdLen=0; //等待接收字節(jié)數(shù) volatile uCHAR get_SendBuf[32]; //發(fā)送數(shù)據(jù)綬沖區(qū) volatile uCHAR get_RecvBuf[32]; //接收數(shù)據(jù)緩沖區(qū) void PORT_Init(void) { PORTB = 0xFF; DDRB = 0x00; PORTC = 0xFF; //M103.html">M103 OUTPUT ONLY DDRC = 0x00; PORTD = 0xFF; DDRD = 0x00; } /***********接收中斷*****************************/ SIGNAL(SIG_UART_RECV) { uCHAR c=UDR0; if(get_bbfferRxdLen>0) { get_RecvBuf[get_bbfferRxdPos++]=c; get_bbfferRxdLen--; } } //是否接收完成 uCHAR RecvComplete(void) { return get_bbfferRxdLen==0; } /***********發(fā)送中斷*****************************/ SIGNAL (SIG_UART_TRANS) { if(--get_bbfferTxdLen>0) UDR0=get_SendBuf[++get_bbfferTxdPos]; } /***********從發(fā)送緩沖區(qū)發(fā)送指定長度數(shù)據(jù)**********/ void SendToUart(uCHAR size) { get_bbfferTxdPos=0; get_bbfferTxdLen=size; UDR0=get_SendBuf[0]; while(get_bbfferTxdLen>0); } /***********接收指定長度數(shù)據(jù)到接收緩沖區(qū)**********/ void RecvFromUart(uCHAR size,uCHAR bwait) { get_bbfferRxdPos=0; get_bbfferRxdLen=size; if(bwait) while(get_bbfferRxdLen>0); } //標(biāo)準(zhǔn)I/O輸出函數(shù) int put_CHAR(CHAR c) { while(!(UCSR0A &(1<<UDRE0))); UDR0 = c; UCSR0A |= (1<<UDRE0); return 0; } //發(fā)送字符串 void put_str(uCHAR *Str) { while(*Str != '\0') { put_CHAR(*Str); Str++; } } /***********uart 初始化**************************/ // 晶振頻率 : 8.0MHz // 通信參數(shù): 8 Data, 1 Stop, No Parity // 波特率: // UBRR0L= 0x0C 19200; // UBRR0L= 0x19 9600; // UBRR0L= 0x33 4800; // UBRR0L= 0x67 2400 void USART_Init(void ) { // 設(shè)置波特率 UBRR0H = 0x00; UBRR0L = 0x33;//9600 baud 8MHZ:51 u2x0=0 103 u2x0=1 //接收中斷允許、發(fā)送中斷允許、接收使能、發(fā)送使能 UCSR0B |= (1<<RXCIE0)|(1<<TXCIE0)|(1<<RXEN0)|(1<<TXEN0); UCSR0C|= (3<<UCSZ00); //8位數(shù)據(jù)位+1位停止位,異步操作 } int main( void ) { uCHAR i; PORT_Init(); USART_Init() ; sei();//總中斷允許 put_str(" init ok ") ; _delay_ms(10); while(1) { //異步接收16 字節(jié)數(shù)據(jù) RecvFromUart(32,0); //等待接收完成 while(!RecvComplete()); //將接收到的數(shù)據(jù)復(fù)制到發(fā)送緩沖區(qū) for(i=0;i<32;i++) get_SendBuf[i]=get_RecvBuf[i]; //發(fā)送回接收到的數(shù)據(jù) SendToUart(32); } } 我的程序停止在put_str(" init ok ") ;這里,串口不斷接收到inin ok,應(yīng)該只接收到1次!看例子有說FUSE的M103兼容模式不對,我用的mega88沒見到過這個熔絲位。 |
5樓: | >>參與討論 |
作者: zx1221 于 2006/9/16 15:09:00 發(fā)布:
我也想要這個功能 ares_lan 要是搞定了說一下啊 |
6樓: | >>參與討論 |
作者: ares_lan 于 2006/9/20 16:26:00 發(fā)布:
并沒有完全搞定啊 這幾天搞別的東西去了。雖然說有進(jìn)展,但還是有問題!改過后的程序去掉了發(fā)送中斷。然后加了一個手動發(fā)送的程序。 //ICC-AVR application builder : 2006-9-14 13:25:47 // Target : M8 // CRYSTAL: 8.0000Mhz #include <iom8v.h> #include <macros.h> #define uCHAR unsigned CHAR #define uint unsigned int #define LedOn PORTC &=~ (1<<PC0) #define LedOff PORTC |= (1<<PC0) #define DeSend PORTD |= (1<<PD4) //允許485發(fā)送 #define DeRece PORTD &=~(1<<PD4) //允許485接收 uCHAR subsen[8]={0xff,0xff,0x00,0x00,0x01,0x01,0x04,0x04}; uCHAR Error[5]={0xff,0xff,0xff,0xff,0xff}; void port_init(void) { PORTB = 0x3F; DDRB = 0xFF; PORTC = 0x00; //M103 OUTPUT ONLY DDRC = 0xff; PORTD = 0x0c; DDRD = 0x1c; } //UART0 initialize // desired baud rate: 2400 // actual: baud rate:2404 (0.2%) // CHAR size: 8 bit // parity: Disabled void uart0_init(void) { UCSRB = 0x00; //disable while setting baud rate UCSRA = 0x00; UCSRC = BIT(URSEL) | 0x0E; UBRRL = 0xCF; //set baud rate lo //2400 UBRRH = 0x00; //set baud rate hi // UBRRL = 0x9f; //1200 // UBRRH = 0x01; UCSRB = 0x98; } void SendMess(uCHAR *data,uCHAR length) { uCHAR i=0; DeSend; while(i <= data[5]+7) { UDR = data[i++]; while(!(UCSRA & (1<<UDRE))); } DeRece; } #pragma interrupt_handler uart0_rx_isr:12 void uart0_rx_isr(void) { subsen[NUM++] = UDR; if(NUM>=5) if(NUM >= subsen[5]+7) //接收到的命令字串為傳感器的 包括一位LRC值 // if(NUM>=subsen[9]+12) //接收到的命令字串為分站的 包括兩位CRC值 { NUM=0; READOVER=1; } } //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts port_init(); timer1_init(); uart0_init(); adc_init(); MCUCR = 0x0A; GICR = 0xC0; TIMSK = 0x04; //timer interrupt sources SEI(); //re-enable interrupts //all peripherals are now initialized } void main(void) { uCHAR i; init_devices(); //insert your functional code here... DeRece; //允許487接收 while(1) { if(READOVER) { READOVER=0; SendMess(subsen,8); LedOff; } } } 其中的SendMess();中,發(fā)送9次才能出8個數(shù)據(jù)。第0次發(fā)送的數(shù)據(jù)不是我想發(fā)的數(shù)。要是只發(fā)送8次,那么最后一個數(shù)據(jù)就收不到,也就是說只能收到7個數(shù)據(jù)。為這個問題搞得比較頭大了。 |
|
|
免費注冊為維庫電子開發(fā)網(wǎng)會員,參與電子工程師社區(qū)討論,點此進(jìn)入 |
Copyright © 1998-2006 www.udpf.com.cn 浙ICP證030469號 |