历史上的今天
今天是:2024年11月16日(星期六)
2019年11月16日 | avr单片机双通信C程序
2019-11-16 来源:51hei
将下面2个程序分别烧录进2个单片机中.
单片机U1
//ICC-AVR application builder : 2013-6-14 10:17:58
// Target : M16
// Crystal: 8.0000Mhz
#include #include #define LED1_ON PORTD|=(1< unsigned char table[]={0XC0,0XF9,0XA4,0xB0,0x99,0x92,0x82,0xF8,0X80,0X90,0X88,0X83,0XC6,0XA1,0X86,0X8e}; unsigned char count=0,count_h,count_l; unsigned char a=0,cnt,cnt1; unsigned char key,count_temp; void port_init(void) { PORTA = 0xFF; DDRA = 0x00; PORTB = 0xFF; DDRB = 0x00; PORTC = 0xFF; //m103 output only DDRC = 0xFF; PORTD = 0xFF; DDRD = 0x32; } //TIMER0 initialize - prescale:256 // WGM: Normal // desired value: 0.005Sec // actual value: 0.005Sec (0.2%) void timer0_init(void) { TCCR0 = 0x00; //stop TCNT0 = 0x64; //set count OCR0 = 0x9C; //set compare TCCR0 = 0x04; //start timer } #pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF void timer0_ovf_isr(void) { a++; TCNT0 = 0x64; //reload counter value } //UART0 initialize // desired baud rate: 9600 // actual: baud rate:9615 (0.2%) void uart0_init(void) { UCSRB = 0x00; //disable while setting baud rate UCSRA = 0x00; UCSRC = BIT(URSEL) | 0x06; UBRRL = 0x33; //set baud rate lo UBRRH = 0x00; //set baud rate hi UCSRB = 0x98; } #pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC void uart0_rx_isr(void) { //uart has received a character in UDR unsigned char udr0; udr0=UDR; count_temp=udr0; } //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts port_init(); timer0_init(); uart0_init(); MCUCR = 0x00; GICR = 0x00; TIMSK = 0x01; //timer interrupt sources SEI(); //re-enable interrupts //all peripherals are now initialized } //发送数据uart0_TX //函数功能:把要发送的数据data送到发送数据缓存器UDR中,然后发送出去 void uart0_TX(unsigned char data) { while(!(UCSRA&(1< } //8M晶振频率下的延时一毫秒 void delay(unsigned int k) { unsigned int m,n; for(m=0;m for(n=0;n<1140;n++) {;} } } //数码管显示函数 void display(void) { count_h=count/10; count_l=count%10; PORTC=table[count_h]; LED1_ON; delay(1);//这个延时很重要,不加的话CUP运行过快将会导致LED数码管无法显示; LED1_OFF; PORTC=table[count_l]; LED2_ON; delay(1); LED2_OFF; } //主函数 void main(void) { init_devices(); while(1) { key=PINB; uart0_TX(key); display(); switch(count_temp) { case 1: TIMSK = 0x01;break; case 2: TIMSK = 0x00;break; case 3: count=0X00;TIMSK = 0x01;break; } if(a==150) { a=0; if(count==59) { count=0; } else { count++; } } } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 单片机U2 //ICC-AVR application builder : 2013-6-14 10:31:18 // Target : M16 // Crystal: 8.0000Mhz #include #include unsigned char led_dis; unsigned char count_temp,cnt1,cnt=1; void port_init(void) { PORTA = 0x00; DDRA = 0x00; PORTB = 0xFF; DDRB = 0xFF; PORTC = 0x00; //m103 output only DDRC = 0x00; PORTD = 0xFF; DDRD = 0x02; } //UART0 initialize // desired baud rate: 9600 // actual: baud rate:9615 (0.2%) void uart0_init(void) { UCSRB = 0x00; //disable while setting baud rate UCSRA = 0x00; UCSRC = BIT(URSEL) | 0x06; UBRRL = 0x33; //set baud rate lo UBRRH = 0x00; //set baud rate hi UCSRB = 0x98; } #pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC void uart0_rx_isr(void) { //uart has received a character in UDR unsigned char udr0; udr0=UDR; led_dis=udr0; } #pragma interrupt_handler int0_isr:iv_INT0 void int0_isr(void) { //external interupt on INT0 cnt++; if(cnt==1) { count_temp=1; } else if(cnt==2) { count_temp=2; cnt=0; } } #pragma interrupt_handler int1_isr:iv_INT1 void int1_isr(void) { //external interupt on INT1 count_temp=3; } //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 = 0x0A; GICR = 0xC0; TIMSK = 0x00; //timer interrupt sources SEI(); //re-enable interrupts //all peripherals are now initialized } //发送数据函数 void USART_TX(unsigned char data) { while(!(UCSRA&(1< } //主函数 void main(void) { init_devices(); while(1) { cnt1=count_temp; USART_TX(cnt1); PORTB=led_dis; } }
史海拾趣
|
中科院计算机技术研究所1999年硕士生入学试题 编译原理与操作系统 一.(15分)有表达式如下:A+B*(C-D)**N (**为幂乘) (1)给出该表达式的逆波兰式表示(后缀式); (2)给出上述表达式的四元式和三元式序列. 二.(15分)有C程序如下: main() { printf("%d,%d,%d\\n",10); } (1)试着写出上述printf语句输出的 ...… 查看全部问答> |
|
最新08版PM4040FL的部分升级开关电源应用资料 [ 本帖最后由 yuandayuan6999 于 2010-6-1 18:35 编辑 ]… 查看全部问答> |
|
一个处理机,机器字长未定,有两个基址寄存器(20位),两个通用寄存器组,每组16个寄存器,用两种指令方案:一种指令字长16位,另一种指令字长24位,他们各有什么优缺点?如果选用24位的,基址寄存器还有保留的必要吗? 请高手帮忙解答一下,谢谢 ...… 查看全部问答> |
|
430的P6.0IO口是AD转换的0号通道,如果不接任何电路器件,那么这个IO口的模拟电压是多少?是0吗?我写了个单通道多次转换的代码,用A0通道转换,A0没有接任何东西,参考电压正电压用2.5V和1.5V转换得到的结果没有差别,负参考电压都是用AVss,菜鸟 ...… 查看全部问答> |
|
求问GrContextFontset包含在哪个头文件中啊,一直显示Undefined symbol GrContextFontset (referred from dddisplay.o).,无法正常引用该函数?还有哪位高人能讲一下 Not enough information to list image symbols的原因么?谢谢啦… 查看全部问答> |
|
MSP430G2 DCO Frequencies February 9, 2012, 10:01 pm MSP430 LaunchPad (MSP-EXP430G2) is compatible with many of the TI‘s Value Line series of microcontrollers. For these MCUs, both the Master clock (MCLK) and the sub-main clo ...… 查看全部问答> |
|
MSP430在程序初始化的时候,将P2IFG=0X00,但是仿真的时候却发现,P2IFG=0x38,也就是2.3,2.4,2.5一直的置位的,清除不了。 在程序使用的过程中,都会对中断标志位清0,但是3和5一直都清除不掉!求大侠帮助… 查看全部问答> |




