左瓶程序.doc

上传人:大张伟 文档编号:8984095 上传时间:2021-01-28 格式:DOC 页数:7 大小:34.01KB
返回 下载 相关 举报
左瓶程序.doc_第1页
第1页 / 共7页
左瓶程序.doc_第2页
第2页 / 共7页
左瓶程序.doc_第3页
第3页 / 共7页
左瓶程序.doc_第4页
第4页 / 共7页
左瓶程序.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《左瓶程序.doc》由会员分享,可在线阅读,更多相关《左瓶程序.doc(7页珍藏版)》请在三一文库上搜索。

1、/*/* Name: 电子设计电源题 功能:稳压,电路保护功能 */ /* Designed by : 胡车,蒋睿,张佩小组 Date:2013/9/5 21:09 */* 开发环境: ICCAVR */* PD0确定键,PD1增加键,PD2菜单键,PD7减小键,PD5输出PWM波,PD6接继电器 */* PA0PA2液晶控制,PB口液晶数据口,PA5采样电压输入,PA6采样电流输入 */*/#include #include #include 1602.h#define uchar unsigned char#define uint unsigned int#define Get_Bit(v

2、al, bitn) (val &(1(bitn) ) uint addata;uint rec=1;uchar flag=1;/进入按键中断标志uchar key_flag=0;/按键标志位uchar num_flag=0;/数字改变标志uchar err_flag=0;/出错类型标志uchar delay_flag=0;/适应电压标志uint vol_exp=368;/360理想输出电压 uint num1=1,num2=5;uint k1,k2;uchar cont1=Limited Current:;uchar cont2= . A ; uchar temp1=Voltage: . V

3、,temp2=Current: . A ,temp3=DUTY CYCLE: %;float temp1float=0,temp2float=0;uint temp1int=0,temp2int=0; void port_init(void) PORTA = 0xf8;/f8 DDRA = 0x07; PORTB = 0x00; DDRB = 0xFF; PORTC = 0xFF; /m103 output only DDRC = 0x00; PORTD = 0x8F; DDRD = 0x70;/TIMER1 initialize - prescale:1/ WGM: 5) PWM 8bit

4、fast, TOP=0x00FF/ desired value: 31.25KHz/ actual value: 31.250KHz (0.0%)void timer1_init(void) TCCR1B = 0x00; /stop TCNT1H = 0xFF; /setup TCNT1L = 0x01; OCR1AH = 0x00; OCR1AL = 0x30;/改变A占空比 OCR1BH = 0x00; OCR1BL = 0x5E;/改变B占空比 ICR1H = 0x00; ICR1L = 0xFF; TCCR1A = 0xF1; TCCR1B = 0x09; /start Timer/*

5、延时函数*/void delay_ms(unsigned int xms) int i,j; for(i=0;ixms;i+) for(j=0;j1140;j+); void delay(uint x) int i,j; for(i=0;ix;i+) for(j=0;j200;j+); /按下按键后待处理的程序void key_sol(void)/液晶界面 flag=0; key_flag=1; LcdInit(); WriteChar(1,0,16,cont1); WriteChar(2,0,16,cont2); WriteNum(2,0,ASCIInum1); WriteNum(2,2,A

6、SCIInum2); writecom(0x0f);/开光标显示 writecom(0xc0); while(key_flag!=0) /*确定按键*/ if(Get_Bit(PIND,PD0)=0) delay_ms(20);key_flag=key_flag+1;while(!(Get_Bit(PIND,PD0);/等待按键松开delay_ms(20); switch(key_flag) case 1:/调节电流NUM1 /*增加按键*/ if(Get_Bit(PIND,PD1)=0) num_flag=1; delay_ms(10);num1+;if(num1=10) num1=9;Wr

7、iteNum(2,0,ASCIInum1);writecom(0xc0); while(!(Get_Bit(PIND,PD1);/等待按键松开delay_ms(10); /*减少按键*/if(Get_Bit(PIND,7)=0) num_flag=1; delay_ms(10);num1-;if(num1=0) num1=1;WriteNum(2,0,ASCIInum1);writecom(0xc0); while(!(Get_Bit(PIND,PD7);/等待按键松开delay_ms(10); break; case 2:/调节电流NUM2 writecom(0xc2); /*增加按键*/

8、if(Get_Bit(PIND,PD1)=0) num_flag=1; delay_ms(10);num2+;if(num2=10) num2=9;WriteNum(2,2,ASCIInum2);writecom(0xc2); while(!(Get_Bit(PIND,PD1);/等待按键松开delay_ms(10); /*减少按键*/if(Get_Bit(PIND,7)=0) num_flag=1; delay_ms(10);num2-;if(num2=0) num2=1;WriteNum(2,2,ASCIInum2);writecom(0xc2); while(!(Get_Bit(PIND

9、,PD7);/等待按键松开delay_ms(10); break; case 3: key_flag=0; break; default: key_flag=0; #pragma interrupt_handler int0_isr:iv_INT0void int0_isr(void) delay_ms(10); key_sol(); /while(!(Get_Bit(PIND,PD2);/等待按键松开 delay_ms(10); key_flag=0; flag=1; LcdInit();void init_devices(void) /stop errant interrupts unti

10、l set up CLI(); /disable all interrupts port_init(); timer1_init(); MCUCR = 0x02; GICR = 0x40; TIMSK = 0x00; /timer interrupt sources SEI(); /re-enable interrupts /all peripherals are now initializeduint AD_GetData(uint ad_input) ADMUX|=0x40; /ADC参考电压AVcc ADCSRA|=0x87; /使能ADC,128分频 ADMUX=ad_input; /

11、选择AD转换通道 ADCSRA|=(1ADSC);/开始AD转换 delay(1); while(!(ADCSRA&(1ADIF); /等待转换结束 ADCSRA|=(1ADIF); /关闭AD转换 ADCSRA&=0x0f; return ADC;/*系统修正*/void sys_comp() while(1) switch (delay_flag) case 0: case 1: if(temp1intvol_exp) OCR1AL=OCR1AL-3; delay_flag+; break; default: if(temp1intvol_exp) OCR1AL=OCR1AL-1; del

12、ay_ms(1); ad_get(); if(OCR1ALvol_exp) delay_ms(30);if(temp1intvol_exp) err_flag=0; err_sol(); PORTD&=0xbf;/打开继电器 if(temp2int(num1*100+num2*10)&(OCR1AL150) err_flag=1; err_sol(); void err_sol() uchar cont3=Safe Mode ; uchar cont4=Caution: ; uchar cont5=Voltage 36V ; switch (err_flag) case 0: PORTD|=(

13、1PD6);/关闭继电器 LcdInit(); WriteChar(1,0,14,cont4); WriteChar(2,0,14,cont5); OCR1AL =2;/初始化A占空比80% delay_flag=0; flag=0; while(!flag);/等待菜单按键按下 delay_ms(50); break; case 1: PORTD|=(1PD6);/关闭继电器 LcdInit(); WriteChar(1,0,14,cont3); / OCR1AL = 0x51;/初始化A占空比80% delay_flag=0; flag=0; while(!flag);/等待菜单按键按下

14、delay_ms(50); break; default: break; /*ad读取*/void ad_get() /uchar ASCII=0123456789; uchar i; uchar t; uchar r; addata=AD_GetData(0x45); /获得通道5数字量 for(i=0;i9;i+) addata=addata+AD_GetData(0x45); temp1float=addata*0.0048828125; temp1float=temp1float/9.08; temp1int=(uint)(temp1float*100)+1); temp18=ASCI

15、Itemp1int/100; temp19=ASCII(temp1int%100)/10; temp111=ASCIItemp1int%10; while(!flag); WriteChar(1,0,17,temp1); addata=AD_GetData(0x46); /获得通道6的数字量 for(i=0;i9;i+) addata=addata+AD_GetData(0x46); temp2float=addata*0.0048828125; temp2float=temp2float/21.0; temp2int=(uint)(temp2float*100); temp28=ASCIIt

16、emp2int/100; temp210=ASCII(temp2int%100)/10; temp211=ASCIItemp2int%10; while(!flag); WriteChar(2,0,17,temp2); r+; if(r=30) /LcdInit(); /* addata=AD_GetData(0x47); /获得通道7的数字量 for(i=0;i9;i+) addata=addata+AD_GetData(0x47); temp3float=addata*0.0048828125; temp3float=temp3float/21.0; temp3int=(uint)(temp3float*100); temp32=ASCIItemp3int/100; temp34=ASCII(temp3int%100)/10; temp35=ASCIItemp3int%10; while(!flag); WriteChar(2,8,7,temp3); */ /WriteChar(2,0,?,temp3);/显示占空比 delay_ms(1);/*主函数*/void main(void) SREG|=0x80; DDRA=0xff; PORTA=0xff; init_devices(); LcdInit(); while(1) sys_comp();

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1