TXT

ISP IO AD

By Ann Hernandez,2014-04-22 20:09
24 views 0
ISP IO AD

    /***********************************************************************************************************

     ** STC12C5A60S2的头文件 **

     ** 引用#include<STC12.h> **

    ***********************************************************************************************************/

    #include<reg52.h>

    #include<string.h>

    //#include<STC12.h>

    //------------------------------------------------------------------------------------------

    sfr IAP_CONTR =0xC7; //ISPCONTR的地址C510xE7,120xC7

    //-------------------------------------------------------------------------- sfr WDT_CONTR = 0xC1; //Watch-Dog-Timer Control register

    // 7 6 5 4 3 2 1 0 Reset Value

    // WDT_FLAG - EN_WDT CLR_WDT IDLE_WDT PS2 PS1 PS0 xx00,0000

    //-------------------------------------------------------------------------- sfr AUXR1 = 0xA2; //Auxiliary Register1 - PCA_P4 SPI_P4 S2_P4 GF2 ADRJ - DPS 0000,0000

    sfr P1ASF = 0x9D; //P1 analog special function

    sfr ADC_CONTR = 0xBC; //A/D 转换控制寄存器 ADC_POWER SPEED1 SPEED0 ADC_FLAG

    ADC_START CHS2 CHS1 CHS0 0000,0000

    sfr ADC_RES = 0xBD; //A/D 转换结果高8 ADCV.9 ADCV.8 ADCV.7 ADCV.6 ADCV.5 ADCV.4

    ADCV.3 ADCV.2 0000,0000

    sfr ADC_RESL = 0xBE; //A/D 转换结果低2

    //------------------------------------------------------------------------------------------

    //sfr PCON = 0x87; //Power Control SMOD SMOD0 LVDF POF GF1 GF0 PD IDL 0001,0000

    //-------------------------------------------------------------------------- sfr WAKE_CLKO = 0x8F;

    /*

     7 6 5 4 3 2 1 0 Reset Value

     PCAWAKEUP RXD_PIN_IE T1_PIN_IE T0_PIN_IE LVD_WAKE _ T1CLKO T0CLKO 0000,0000B

b7 - PCAWAKEUP : PCA 中断可唤醒 powerdown

    b6 - RXD_PIN_IE : P3.0(RXD) 下降沿置位 RI 时可唤醒 powerdown(必须打开相应中断)

    b5 - T1_PIN_IE : T1 脚下降沿置位 T1 中断标志时可唤醒 powerdown(必须打开相应中

    )

    b4 - T0_PIN_IE : T0 脚下降沿置位 T0 中断标志时可唤醒 powerdown(必须打开相应中

)

    b3 - LVD_WAKE : CMPIN 脚低电平置位 LVD 中断标志时可唤醒 powerdown(必须打开相应

    中断)

    b2 -

    b1 - T1CLKO : 允许 T1CKO(P3.5) 脚输出 T1 溢出脉冲?Fck1 = 1/2 T1 溢出率

    b0 - T0CLKO : 允许 T0CKO(P3.4) 脚输出 T0 溢出脉冲?Fck0 = 1/2 T1 溢出率

    */

    //sfr IE = 0xA8; //中断控制寄存器 EA ELVD EADC ES ET1 EX1 ET0 EX0 0x00,0000

    //------------------------------------------------------------------------------

    -------------

    //sbit EA = IE^7;

    sbit ELVD = IE^6; //低压监测中断允许位

    sbit EADC = IE^5; //ADC 中断允许位

    /*

    sbit ES = IE^4;

    sbit ET1 = IE^3;

    sbit EX1 = IE^2;

    sbit ET0 = IE^1;

    sbit EX0 = IE^0;

    */

    //------------------------------------------------------------------------------

    -------------

    sfr IE2 = 0xAF; //Auxiliary Interrupt - - - - - - ESPI ES2

    0000,0000B

    //------------------------------------------------------------------------------

    -------------

    // 7 6 5 4 3 2 1

    0 Reset Value

    //sfr IP = 0xB8; //中断优先级低位 PPCA PLVD PADC PS PT1 PX1 PT0 PX0 0000,0000

    //------------------------------------------------------------------------------

    --------------

    sbit PPCA = IP^7; //PCA 模块中断优先级

    sbit PLVD = IP^6; //低压监测中断优先级

    sbit PADC = IP^5; //ADC 中断优先级

    /*

    sbit PS = IP^4;

    sbit PT1 = IP^3;

    sbit PX1 = IP^2;

    sbit PT0 = IP^1;

    sbit PX0 = IP^0;

    */

    // 7 6 5 4 3 2

1 0 Reset Value

    sfr IPH = 0xB7; //中断优先级高位 PPCAH PLVDH PADCH PSH PT1H PX1H PT0H PX0H 0000,0000

    sfr IP2 = 0xB5; // - - - - - -

    PSPI PS2 xxxx,xx00

    sfr IPH2 = 0xB6; // - - - - - -

    PSPIH PS2H xxxx,xx00

    /*******************************************************************************

    ******************************

     MCU程序段

********************************************************************************

    *****************************/

    #define uchar unsigned char

    #define uint unsigned int

    sbit buzzer=P2^7;

    void delay(uint ms)

    {

     uint t;

     for(;ms>0;ms--)

     for(t=330;t>0;t--);

    }

    /*************************************************************************

     系统初始化

**************************************************************************/

    #define PCON_POWERDOWN 0x02;

    #define PCON_FLVD 0x20;

    #define WAKE_LVD 0x08;

    #define WAKE_RI 0x40;

    #define WDT_CLR 0x10;

    void sys_init()

    {

     EA = 1; //开总中断

    //--------------------------------------------------------- // PCON;电源控制? & WAKE;唤醒?

    // WAKE_CLKO |=WAKE_RI; //Receive & LVD唤醒方式

     WAKE_CLKO |=WAKE_LVD;

    // ES = 1;

     ELVD=1; //LVD中断允许(asdf;sdakfsdaf;ksdfljkajfdsafsadfsdfsdf)

// PCON |=PCON_POWERDOWN; //MCU掉电模式工作

    // ELVD=0; //关中断

    // PCON &=~PCON_FLVD; //中断;标志FLVD?清0

    // PCON &=~PCON_POWERDOWN; //停用掉电模式

    //-----------------------------------------------------------

    // WDT;开门狗?

     WDT_CONTR=0x27; //开启WDTfocs/128 9S 溢出?

    // WDT_CONTR |=WDT_CLR; //WDT0(1)

    //-----------------------------------------------------------

    //bps设置串口通信的波特率;以工作方式18位可变异步通信?

     SCON = 0x50; //0101,0000 8 位可变波特率?无奇偶校验位

     TMOD = 0x20;

     TH1 = 0xfd;

     TL1 = 0xfd;

     TR1 = 1;

     ES = 1;

     PS=1;

    //-----------------------------------------------------------

     buzzer=0;

     delay(1000);

     buzzer=1;

    }

    /********************************************************************

    * A/D start * *********************************************************************/

    uchar ADC_display[16]="A/D CH*: V ";

    uint ADC_data;

    uchar ADC_ch;

    uchar ADC_busy=0;

    #define ADC_POWER 0x80;

    #define ADC_FLAG 0x10;

    #define ADC_START 0x08;

    #define ADC_SPEEDHH 0x60; //90个周期转换

    #define ADC_SPEEDH 0x40; //180个周期转换

    #define ADC_SPEEDL 0x20; //360个周期转换

    #define ADC_SPEEDLL 0x00; //540个周期转换

    void ADC_start(uchar ch)

    {

     P1ASF=0xFF; //P1设置成模拟口A/D使用

    // AUXR1=0x00; //10 A/D 转换结果的高8 位放在ADC_RES 寄存器, 2 位放在ADC_RESL 寄存器

     ADC_RES=0x00;

     ADC_RESL=0x00;

     ADC_CONTR=ADC_POWER;

     delay(1);

     ADC_CONTR |=ADC_SPEEDLL;

     ADC_CONTR |=ch;

     ADC_CONTR |=ADC_START;

     ADC_busy=1;

     EADC=1; //ADC中断 }

    void scom_receive(uchar temp_array[])

    {

     uchar i;

     while(RI && i<5)

     {

     temp_array[i]=SBUF;

     RI=0;

     i++;

     delay(5);

     }

    }

    void scom_transmit(char *ps)

    {

     while(*ps)

     {

     SBUF=*ps;

     ps++;

     while(!TI);

     TI=0;

     }

    }

    void IO_state() {

     SBUF=P0;

     while(!TI);

     TI=0;

     SBUF=P1;

     while(!TI);

     TI=0;

     SBUF=P2;

     while(!TI);

     TI=0;

     SBUF=P3;

     while(!TI);

     TI=0;

    }

    /*******************************************************************************

     IAP_CONTR: IAP 控制寄存器?

********************************************************************************

    **/

    /*7bit ISPEN: ISP/IAP 功能允许位。0,禁止ISP/IAP 编程改变Flash,1:允许编程改变Flash 6bit SWBS: 软件选择从用户主程序区启动;0 ??还是从I S P 程序区启动;1 5bit SWRST: 0: 不操作! 1: 产生软件系统复位?硬件自动清零。

    软启到ISP下载程序到ROM*/

    void soft_reset_to_IAP_monitor()

    {

     buzzer=0;

     delay(2000);

     IAP_CONTR=0x60; //ISP_CONTR 0110,0000 软启到ISP

    }

    /***************************************************************************

     ISP下载 receive_arraypassword_array时复位到ISP下载

****************************************************************************/

    uchar password_array[]={0x88,0x05,0x22,0x00}; //Self通信命令码880528 void receive_deal()

    {

     uchar receive_array[5]=""; //接收到的通信命令码!

     ES=0;

     scom_receive(receive_array);

    // scom_transmit(receive_array);

     if(strstr(password_array,receive_array) || strstr("522",receive_array))

     {

     soft_reset_to_IAP_monitor();

     }

     else if(strstr("IO",receive_array) )

     {

     IO_state();

     }

     else if(ADC_busy==0 && strcmp(receive_array,"AD")>=0)

     {

     if(receive_array[2]>='0' && receive_array[2]<'8')

     {

     TR0=0; //P1口在AD模式下工作?关闭

     ADC_ch=receive_array[2]-48;

     ADC_start(ADC_ch);

     }

     }

     ES=1;

    }

    void ADC_RES_deal()

    {

     uint temp;

     uchar i;

     EADC=0;

     ADC_CONTR=0x00;

     P1ASF=0x00;

     ADC_data=ADC_RES; //A/D 转换结果高8 ADCV.9 ADCV.8 ADCV.7 ADCV.6 ADCV.5

    ADCV.4 ADCV.3 ADCV.2 0000,0000

     ADC_data=(ADC_data<<2) | ADC_RESL; //A/D 转换结果低2

     ADC_display[6]=ADC_ch+48;

     temp=ADC_data;

     for(i=4;i>0;i--)

     {

     ADC_display[7+i]=temp % 10+48;

     temp /=10;

     }

     scom_transmit(ADC_display);

     ADC_busy=0;

     TR0=1;

    }

    /***************************************************************************

     键盘 P0作为4*4矩阵键盘用时的Key_scan 函数

****************************************************************************/

    char Key[4][5]=

    {

     "123A",

     "456B",

     "789C",

     "*0#D"

    };

    void Key_scan()

    {

     static uchar Key_flag=0;

     uchar temp;

     temp=P2;

     P2=0xF0;

     if(Key_flag==1)

     {

     if(P2==0xF0)

     Key_flag=0;

     }

     else

     {

     if(P2!=0xF0)

     {

     uchar Key_H,Key_L,i=1,j=1;

     Key_flag=1;

     Key_H=(~P2)>>4;

     while(Key_H>>i)

     i++;

     P2=0x0F;

     Key_L=(~P2) & 0x0F;

     while(Key_L>>j)

     j++;

     ES=0;

     SBUF=Key[i-1][j-1];

     while(!TI);

     TI=0;

     ES=1;

     if(Key[i-1][j-1]>48 && Key[i-1][j-1]<57) //1~8

     {

     P1=~(0x01<<(Key[i-1][j-1]-49));

     }

     else

     {

     switch(Key[i-1][j-1])

     {

     case '0': P1=0xFF;break;

     case '9': P1=0x00;break;

     case '*':

     buzzer=0,delay(300),buzzer=1,delay(200);

     buzzer=0,delay(300),buzzer=1;break; //'*'

     case '#': soft_reset_to_IAP_monitor(); //ISP重启下载

     default : ;

     }

     }

     if(Key[i-1][j-1]!='*')

     buzzer=0,delay(100),buzzer=1;

     }

     }

     P2=temp;

    }

Report this document

For any questions or suggestions please email
cust-service@docsford.com