PIC16F873 ADC & PWM & EEPROM
/共6個按鍵分別定義為:1-6;利用RA0作為ADC模擬采樣轉(zhuǎn)換;將5V電源均分成6等分
//當(dāng)相應(yīng)鍵按下時,數(shù)碼管會顯示鍵值,RC2/CCP1會輸出相應(yīng)脈寬的PWM波形,一直到有其他鍵按下改變其值為止
//當(dāng)芯片啟動,沒按過鍵的情況下,RC2/CCP1輸出0.5脈寬的PWM波形
#include
unsigned char vh,vl;
void adc_init(void);
void pwm_init(void);
void interrupt adc(void);
void numbers(unsigned char o);
void eepromwrite(unsigned char addr,unsigned char value);
unsigned char eepromread(unsigned char addr);
void main()
{
unsigned int i,v;
unsigned char j1,j2,a1,a2;
TRISB=0x00;
PORTB=0x7E;
adc_init();
pwm_init();
j1=eepromread(0x11);
vl=eepromread(0x12);
vh=eepromread(0x13);
CCPR1L=vh; CCP1CON=vl;
numbers(j1);
GIE=1;
while(1)
{
vh=CCPR1L;
vl=CCP1CON;
a1=vl; a2=vh;
ADGO=1; //啟動A/D轉(zhuǎn)換啟動兼狀態(tài)位,手冊上寫作GO/DONE
while(ADGO); //等待轉(zhuǎn)換完成
for(i=500;i!=0;i--) ; //保證采樣延時
if(a1!=vl||a2!=vh)
{
j1=0;
a1=vl; a2=vh;
a1>>=5; v=vh;
v<<=2; v+=a1;
for(i=1;i<11;i++)
if(v>102*i) j1++;
if(j1!=10)
{
if(j1==1) j2=1;
if(j1==2) j2=2;
if(j1==4) j2=3;
if(j1==5) j2=4;
if(j1==7) j2=5;
if(j1==9) j2=6;
numbers(j2); j2=j1;
CCPR1L=vh; CCP1CON=vl;
eepromwrite(0x11,j2);
eepromwrite(0x12,vl);
eepromwrite(0x13,vh);
}
}
}
}
void adc_init(void)
{
ADCON0=0x41; //第7-6位ADCS<1:0>:01,頻率為f/8;第5-3位CHS<2:0>:000,選擇通道RA0/AN0;
//第2位A/D轉(zhuǎn)換啟動兼狀態(tài)位ADGO=0;0位ADON=1,A/D轉(zhuǎn)換器開關(guān)位
ADCON1=0x0E; //ADMF=0,ADRESH用于存放A/D轉(zhuǎn)換結(jié)果的高8位,ADRESL用于存放A/D轉(zhuǎn)換結(jié)果的低2位,
//只有RA0/AN0作為模擬通道,其余均為數(shù)值通道
TRISA0=1; //定義RA0口作為輸入,其余RA口作為輸出
PIR1=0x00; //6位ADC模塊中斷標(biāo)志位ADIF=0;
PIE1=0x40; //6位ADC模塊中斷使能位ADIE=1;
INTCON=0x40; //7位全局中斷標(biāo)志位GIE=0;6位外部中斷標(biāo)志位PEIE=1;
}
void pwm_init(void)
{
CCP1CON=0x3C; //第5位與第4位CCP1X=CCP1Y=1;CCP1M<3:0>=1100:CCP1工作于PWM工作模式
CCPR1L=0x7f; //PWM脈寬=CCPR1L:CCP1CON<5:4>×T×TMR2的預(yù)分頻值
PR2=0xff; //設(shè)置PWM的工作周期=(PR2+1)×4T×TMR2的預(yù)分頻值,即為T2CKPS<1:0>值
TRISC2=0;
T2CON=00000100; //TIMER2控制寄存器,第6-3位TOUTPS<3:0>=0000=1:1后分頻比;2位計數(shù)器Timer2全能位TMR2ON=1;
//第2-0位T2CKPS<1:0>=00,TMR2的預(yù)分頻比為1; 等同于T2CON=0b00000100=0x04
}
void interrupt adc(void)
{
if(ADIF==1)
{
CCP1Y=0;
CCP1X=0; //ADC結(jié)果高低位寄存器ADRESH,ADRESL
vl|=((ADRESL&0xc0)>>2);
vh=ADRESH;
ADIF=0;
}
}
void numbers(unsigned char o)
{
switch(o)
{
case 0: PORTB=0x7E; break; //0
case 1: PORTB=0x0C; break; //1
case 2: PORTB=0xB6; break; //2
case 3: PORTB=0x9E; break; //3
case 4: PORTB=0xCC; break; //4
case 5: PORTB=0xDA; break; //5
case 6: PORTB=0xFA; break; //6
case 7: PORTB=0x0E; break; //7
case 8: PORTB=0xFE; break; //8
case 9: PORTB=0xDE; break; //9
case 10: PORTB=0xEE; break; //A
case 11: PORTB=0xF8; break; //B
case 12: PORTB=0x72; break; //C
case 13: PORTB=0xBC; break; //D
case 14: PORTB=0xF2; break; //E
case 15: PORTB=0xE2; break; //F
default: PORTB=0x40; //-
}
}
void eepromwrite(unsigned char addr,unsigned char value)
{
EEADR=addr; //給地址
EEDATA=value; //給數(shù)據(jù)
EEPGD=0; //選擇EEPROM 存儲器
WREN=1; //使能寫
GIE=0; //禁止中斷
EECON2=0x55;
EECON2=0xAA; //寫規(guī)定例程
WR=1; //開始寫
while(WR); //等待寫完
GIE=1; //恢復(fù)中斷
WREN=0; //禁止寫
}
unsigned char eepromread(unsigned char addr)
{
unsigned char evlaue;
GIE=0;
EEADR=addr;
EEPGD=0;
RD=1; //開始讀
evlaue=EEDATA;
GIE=1;
return(evlaue);
}
//EECON1:<7>EEPGD存儲器選擇位,=1選擇程序存儲器,=0選擇數(shù)據(jù)存儲器;<3>WRERR EEPROM寫錯誤標(biāo)志,=1寫提前結(jié)束,=0寫完成;
// <2>WREN EEPROM寫使能位,=1允許重復(fù)寫,=0不可寫;<1>WR寫控制位,=1正在寫,=0寫完成;<0>RD讀控制位,=1正在讀,=0讀完成
//EECON2:僅是一個邏輯上的寄存器,而不是一個物理上存在的寄存器,讀出時將總是為零。它只
//在寫操作時起作用。