全部资料51hei下载地址:
液晶3(可用3).zip
(665.28 KB, 下载次数: 16)
单片机源程序如下:
- #include "derivative.h"
- #include <stdio.h>
- #include <string.h>
- #include"OLED12864.h"
- int AD_wValue[6];//AD转换结果
- word AD[6]={0,0,0,0,0,0};
- word AD_erro[4]={0,0,0,0};
- int i;
- //word AD_value[9]={0,0,0,0,0,0,0,0,0};
- int i,numbermax,AD_valuemax,AD_valuemax2,Remax,Remax2;
- int numbermax2,Expect_value,Expect_value2,Renumber,Renumber2;
- float temp,angle;
- int directflag;
- char out_flag1,out_flag0;
- //char out_flag0,out_flag1;
- void delay(int n)
- {
- int i,j;
- for(i=0;i<n;i++);
- }
-
- //-----时钟初始化程序--------//
- void PLL_Init(void)
- {
- REFDV=1;
- SYNR=2;
- while(!(CRGFLG&0x08));
- CLKSEL=0x80;
- }
- //---------------------------------------------------------------------
- // 函数功能:配置单片机锁相环,使其工作在40Mhz
- // 形式参数: 无
- // 函数返回值:无
- //---------------------------------------------------------------------
- void BusCLK_40M(void)
- {
- CLKSEL=0X00; //disengage PLL to system
- PLLCTL_PLLON=1; //turn on PLL
- SYNR =0xc0 | 0x04;
- REFDV=0x80 | 0x01;
- POSTDIV=0x00; //pllclock=2*osc*(1+SYNR)/(1+REFDV)=80MHz;
- _asm(nop); //BUS CLOCK=40M
- _asm(nop);
- while(!(CRGFLG_LOCK==1)); //when pll is steady ,then use it;
- CLKSEL_PLLSEL =1; //engage PLL to system;
- }
- void Init_PWM(void)
- {
- PWME=0X00;
- PWMCTL_CON01=1; //通道级联
- PWMCTL_CON23=1;
- PWMCTL_CON45=1;
- PWMCAE=0X00; //0左对齐 1中心对齐
- PWMPRCLK=0X22; //2分频
- PWMPOL_PPOL1=1;
- PWMPOL_PPOL5=1;
- PWMPOL_PPOL3=1;
- //PWMCNT01 = 0; //计数器从零开始计数
- //PWMCNT23 = 0; //计数器从零开始计数
-
- PWMSCLA=1;
- PWMSCLB=0X19;
- PWMCLK_PCLK1=0;
- PWMCLK_PCLK5=1;
- PWMCLK_PCLK3=1;
- //PWMPRCLK =0x00; //选择时钟B,50MHZ
-
- PWMCLK=0XFF;
-
- PWMPER01 = 1000; //周期 20ms
- PWMPER45 = 1000; //周期 20ms
- PWMPER23=4000; //200hz 周期0.005ms
-
- PWMDTY01 = 670;
- PWMDTY45 = 670;
- PWMDTY23=315; //317zhong 347zuo 280you
-
-
- PWME_PWME3 = 1;
- PWME_PWME1 = 1;
- PWME_PWME5=1;
- }
- //--------------总线时钟=48/2=24MHz------------//
- //-----ATD初始化程序--------//
- void AD_Init(void)
- {
- ATD0CTL1=0x00; //00000000 选择AD0通道为外部触发,8 位精度,采样前不放电
- ATD0CTL2=0x40; //01000000 标志位自动清零,禁止外部触发, 禁止中断
- ATD0CTL3=0xb0; //10001000 右对齐无符号,每次转换1 个序列, No FIFO, Freeze 模式下继续转
- ATD0CTL4=0x03; //00000001 采样时间为4 个AD 时钟周期,PRS=1,ATDClock=24/(2*(1+1))=6MHz
- ATD0CTL5=0x30; //00100000 特殊通道禁止,连续转换,单通道转换,输入通道为0
- ATD0DIEN=0x00; //00000000 禁止数字输入
- }
- //-----读取AD转换结果--------//
- void AD_GetValue()
- {
- AD_wValue[0]=ATD0DR0; //读取结果寄存器的值
- AD_wValue[1]=ATD0DR1; //读取结果寄存器的值
- AD_wValue[2]=ATD0DR2; //读取结果寄存器的值
- AD_wValue[3]=ATD0DR3; //读取结果寄存器的值
- AD_wValue[4]=ATD0DR4;
- AD_wValue[5]=ATD0DR5;
-
- }
- void remember_ADvalue()
- {
- AD[0]=AD_wValue[0];
- AD[1]=AD_wValue[1];
- AD[2]=AD_wValue[2];
- AD[3]=AD_wValue[3];
- AD[4]=AD_wValue[4];
- AD[5]=AD_wValue[5];
- }
-
- void xiuzhen_ADvalue()
- {
- for(i=0;i<=5;i++)
- AD_erro[i]=AD_wValue[i]-AD[i];
- if(AD_erro[i]>10)
- AD_wValue[i]=AD[i];
- else
- AD_wValue[i]=AD_wValue[i];
- }
-
- void Get_Road()
- {
- for(i=0;i<=3;i++)
- {
- if(AD[i]>AD_valuemax)
- {
- AD_valuemax=AD[i]; //采集AD的最大值
- numbermax=i; //记录最大值的通道
- // Remax=AD_valuemax;
- // Renumber=i;
- }
- // else
- // {
- // AD_valuemax=Remax;
- // i=Renumber;
- // }
- }
- for(i=0;i<=3;i++)
- {
- if(AD[i]>AD_valuemax2&&AD[i]<AD_valuemax)
- {
- AD_valuemax2=AD[i];
- numbermax2=i;
- // Remax2=AD_valuemax2;
- // Renumber2=i;
- }
- // else
- //{AD_valuemax=Remax2;
- // i=Renumber2;}
- }
- switch (numbermax) {
- case 0: Expect_value=30; if(AD_valuemax>60)directflag=1;break;
- case 1: Expect_value=10; break;
- case 2: Expect_value=-10; break;
- case 3: Expect_value=-30; if(AD_valuemax>60)directflag=2;break;
- //case 4: Expect_value=-50; break;
- //case 5: Expect_value=-150; if(AD_valuemax>80)directflag=2;break;
- //case 6: Expect_value=-200;if(AD_valuemax>80)directflag=2;break;
- }
- switch (numbermax2) {
- case 0: Expect_value2=30; break;
- case 1: Expect_value2=10; break;
- case 2: Expect_value2=-10; break;
- case 3: Expect_value2=-30; break;
- //case 4: Expect_value2=-50; break;
- //case 5: Expect_value2=-150; break;
- //case 6: Expect_value2=-200; break;
- }
- }
- void change_duoji()
- {
- if(numbermax>numbermax2) temp=(Expect_value+Expect_value2)/2-(AD_valuemax-AD_valuemax2)/2;
- if(numbermax<numbermax2) temp=(Expect_value+Expect_value2)/2+(AD_valuemax-AD_valuemax2)/2;
-
- if(AD_wValue[0]>100||AD_wValue[1]>100||AD_wValue[2]>100||AD_wValue[3]>100)
- {
- if(numbermax<1) {angle=temp*0.30+315;PWMDTY45=100;PWMDTY01=570;}
- else if(numbermax>2) {angle=temp*0.30+315;PWMDTY45=570;PWMDTY01=100;}
- else angle=temp*0.20+315;
- }
-
- else if(AD_wValue[4]>AD_wValue[5]) angle=((AD_wValue[4]-AD_wValue[5])/2)*0.7+325;
- else if(AD_wValue[5]>AD_wValue[4]) angle=305-((AD_wValue[5]-AD_wValue[4])/2)*0.70;
-
- if(angle>350)
- {
- angle=350;
- PWMDTY45=100;
- PWMDTY01=570;
- }
- if(angle<280)
- {
- angle=280;
- PWMDTY45=570;
- PWMDTY01=100;
- }
-
- if(directflag==1&&AD_valuemax2<40) angle=350;
- if(directflag==2&&AD_valuemax2<40) angle=280;
- PWMDTY23=(int)angle;
- AD_valuemax=0;AD_valuemax2=0;
- }
- //-----主函数--------//
- void main(void)
- {
- char e[100] ;
- //PLL_Init();
- BusCLK_40M();
- AD_Init();
- Init_PWM();
- DDRB=0XFF;
- PORTB=0XFF;
- LCD_Init();
- EnableInterrupts;
-
- for(;;)
- {
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
|