资源描述:
《i2c双机通讯的车灯变光设计》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、I2C双机通讯的程序这是本人做的毕业设计——基于AVR单片的智能车灯控制,一些地方还要需要完善的地方,设计在车灯的总线控制还是有问题的,I2C不比CAN总线的抗干扰性和传输速率快的特点,拿来练下手,还是可以的!主机用atmega16做的,子机用atmega8做的,上图!/*****************************************************主机程序:Chiptype:ATmega16Programtype:ApplicationAVRCoreClockfrequency:8.MHzMemorymodel:SmallExternalRAMsize:0D
2、ataStacksize:256*****************************************************/#include#include#defineucharunsignedchar#defineuintunsignedint/*#defineTest_outDDRD.4=1#defineTest_LPORTD.4=0#defineTest_HPORTD.4=1*/#defineBell_outDDRC.7=1#defineBell_HPORTC.7=1#defineBell_LPORTC.7=0#includ
3、e"Lcd12864.h"#include"Twi.h"#include"Keyscan.h"ucharKey_num=0;ucharvalue=128;ucharbi=0;ucharcc=0;voidAD5161_work(){floatmin=0;if(Key_num==2){value+=10;if(value==255){value=0;}Key_num=0;}if(Key_num==3){value-=10;if(value==0){value=255;}Key_num=0;}min=256-value;min=(min/256)*100+0.5;min=(unsignedc
4、har)min;dis_num(min,3,5);Lcd_puts("%");AD5161_writeB(AD5161_address,0,value);}voidMoshi_xuanze(){if(Key_num==1){bi++;if(bi==1){cc=0;Lcd_gotoxy(2,4);Lcd_puts("自动模式");Lcd_gotoxy(4,5);Lcd_puts("无检测");Set_twi(Sensor_address,1);}if(bi==2){cc=1;Lcd_gotoxy(2,4);Lcd_puts("手动模式");Set_twi(Sensor_address,2
5、);bi=0;}Key_num=0;}}voidLight_start(){if(cc==1){if(Key_num==4){Lcd_gotoxy(4,5);Lcd_puts("关闭");Set_twi(Sensor_address,5);Key_num=0;}if(Key_num==5){Lcd_gotoxy(4,5);Lcd_puts("正常");Set_twi(Sensor_address,6);Key_num=0;}if(Key_num==6){Lcd_gotoxy(4,5);Lcd_puts("远光开");Set_twi(Sensor_address,3);Key_num=0
6、;}if(Key_num==7){Lcd_gotoxy(4,5);Lcd_puts("近光开");Set_twi(Sensor_address,4);Key_num=0;}}}voidmain(void){//TWIinitialization//BitRate:100.000kHzTWBR=0x20;TWSR=0x00;Bell_out;Bell_L;Init_12864();Lcd_display();//Set_twi(Sensor_address,num);//delay_ms(20);//Read_twi(0x25);Key_num=1;Moshi_xuanze();whil
7、e(1){Key_num=Key_check();AD5161_work();Moshi_xuanze();Light_start();//num=Key_check();//dis_num(num,1,4);//Set_twi(Sensor_address,num);}}/************************************************************按键扫描程序:*******************