部分代码
#ifndef __BSP_H
#define __BSP_H
#include "reg51.h"
#include "intrins.h"
/**************************ϵͳ²ÎÊýÅäÖÃ*****************************************/
#define FOSC 11059200L //µ¥Æ¬»ú¾§ÕñƵÂÊ
#define BAUD 115200L //´®¿ÚÊä³ö²¨ÌØÂÊ
#define VREF 120
/*****************************end***********************************************/
#define TIMER0_FREQ (1000) // 31.25ms¶¨Ê±Æ÷
#define TIME0_MS (65536-FOSC/12/TIMER0_FREQ)//time0×°ÔØcont¼ÆËã
#define RX_LEN 20
#define TX_LEN 16
#define START 1
#define STOP 0
/*Define ISP/IAP/EEPROM command*/
#define CMD_IDLE 0 //Stand-By
#define CMD_READ 1 //Byte-Read
#define CMD_PROGRAM 2 //Byte-Program
#define CMD_ERASE 3 //Sector-Erase
#define ENABLE_IAP 0x82 //if SYSCLK<20MHz
//Start address for STC12C56xx EEPROM
#define IAP_ADDRESS 0x0400
//ÒÔÏÂΪSTCµÄ¼Ä´æÆ÷µÄºê¶¨Òå
sfr CCON=0xD8; //PCAcontrolregister
sbit CCF0=CCON^0; //PCAmodule-0interruptflag
sbit CCF1=CCON^1; //PCAmodule-1interruptflag
sbit CR=CCON^6; //PCAtimerruncontrolbit
sbit CF=CCON^7; //PCAtimeroverflowflag
sfr CMOD=0xD9; //PCAmoderegister
sfr CL=0xE9; //PCAbasetimerLOW
sfr CH=0xF9; //PCAbasetimerHIGH
sfr CCAPM0=0xDA; //PCAmodule-0moderegister
sfr CCAP0L=0xEA; //PCAmodule-0captureregisterLOW
sfr CCAP0H=0xFA; //PCAmodule-0captureregisterHIGH
sfr CCAPM1=0xDB; //PCAmodule-1moderegister
sfr CCAP1L=0xEB; //PCAmodule-1captureregisterLOW
sfr CCAP1H=0xFB; //PCAmodule-1captureregisterHIGH
sfr CCAPM2=0xDC; //PCAmodule-2moderegister
sfr CCAP2L=0xEC; //PCAmodule-2captureregisterLOW
sfr CCAP2H=0xFC; //PCAmodule-2captureregisterHIGH
sfr CCAPM3=0xDD; //PCAmodule-3moderegister
sfr CCAP3L=0xED; //PCAmodule-3captureregisterLOW
sfr CCAP3H=0xFD; //PCAmodule-3captureregisterHIGH
sfr PCAPWM0=0xF2;
sfr PCAPWM1=0xF3;
sfr PCAPWM2=0xF4;
sfr PCAPWM3=0xF5;
/*Declare SFR associated with the IAP */
sfr IAP_DATA = 0xE2; //Flash data register
sfr IAP_ADDRH = 0xE3; //Flash address HIGH
sfr IAP_ADDRL = 0xE4; //Flash address LOW
sfr IAP_CMD = 0xE5; //Flash command register
sfr IAP_TRIG = 0xE6; //Flash command trigger
sfr IAP_CONTR = 0xE7; //Flash control register
sfr AUXR=0x8e;//Auxiliaryregister
sfr P1M0=0x91;
sfr P1M1=0x92;
sfr P3M0=0xB1;
sfr P3M1=0xB2;
sbit dianji_switch=P1^6;
sbit cizhen_switch=P1^7;
sbit DATA=P3^2;
sbit WR1=P3^3;
sbit CS2=P3^4;
sbit CS1=P3^5;
sbit CS3=P3^7;
//P1.3 ÆôÍ£°´¼ü
//P1.4 ´ÅÕñ°´¼ü
//P1.1ÊÚȨ°´¼ü
//P1.2 ÐýÕñ°´¼ü
sbit k1=P1^3;//´ÅÕñ°´¼ü
sbit k2=P1^1;//ÐýÕñ°´¼ü
sbit k3=P1^4;//ÆôÍ£¿ª¹Ø°´¼ü
sbit k4=P1^2;//ÊÚȨ°´¼ü
extern char tx_buf[TX_LEN];
extern char rx_buf[RX_LEN];
extern unsigned char rx_index;
extern unsigned int timer0_tick;
//º¯ÊýÉùÃ÷
void time0_init(void);
void initiate_RS232 (void);
void SendData(char dat);
void SendString(char *s);
void SendString_len(char *s,unsigned char len);
void PCA_init(void);
void set_PWM1_voltage(unsigned int v);
void set_pwm1_stat(bit flag);
void set_timer0_stat(bit s);
void save_cnt(unsigned int cnt,unsigned int limit);
void get_cnt(unsigned int *cnt,unsigned char addr);
void Delay10ms(); //@11.0592MHz
#endif
***************************************************************************************/
#include "bsp.h"
char tx_buf[TX_LEN]={0};
char rx_buf[RX_LEN]={0};
unsigned char rx_index=0;
unsigned int timer0_tick=0;
bit busy;
//time0³õʼ»¯º¯Êý 1ms¶¨Ê±Æ÷£¬Ò»Ö±´æÔÚ
void time0_init(void)
{
AUXR &= ~0x80; //timer0 work in 1T mode
TMOD |= 0x01; //set timer0 as mode1 (16-bit)
TL0 = TIME0_MS; //initial timer0 low byte
TH0 = TIME0_MS >> 8; //initial timer0 high byte
TR0 = 0; //timer0 STOP running
ET0 = 0; //enable timer0 interrupt
EA = 1; //open global interrupt switch
}
//time0Öжϴ¦Àíº¯Êý
void tm0_isr() interrupt 1 using 1
{
TL0 = TIME0_MS; //reload timer0 low byte
TH0 = TIME0_MS >> 8; //reload timer0 high byte
timer0_tick++;
}
void set_timer0_stat(bit s)
{
TR0 = s; //timer0 STOP running
ET0 = s; //enable timer0 interrupt
timer0_tick=0;
}
//´®¿Ú³õʼ»¯£¬²¨ÌØÂÊÔÚmain.hµÄBAUDµ÷½Ú ʹÓÃTIMER1×÷Ϊ²¨ÌØÂʵÄƵÂÊÔ´
void initiate_RS232 (void)
{
SCON = 0x50; //8-bit variable UART
AUXR |= 0x40; //ÉèÖÃBIT6¼´ÉèÖÃΪ12Tģʽ
TMOD |= 0x20; //Set Timer1 as 8-bit auto reload mode
TH1 = TL1 = 256-(FOSC/32/BAUD); //Set auto-reload vaule
TR1 = 1; //Timer1 start run
ES = 1; //Enable UART interrupt
EA = 1; //Open master interrupt switch
}
//´®¿ÚÖжϴ¦Àíº¯Êý£¬´¦Àí´®¿ÚÊÕ·¢
void Uart_Isr() interrupt 4 using 1
{
if (RI)
{
RI = 0;
busy=1;
rx_buf[rx_index++]=SBUF;
busy=0;
if(rx_index>RX_LEN-1)
{
rx_index=0;
}
}
if (TI)
{
TI = 0;
busy=0;
}
}
void SendData(char dat)
{
while (busy); //Wait for the completion of the previous data is sent
busy = 1;
SBUF = dat; //Send data to UART buffer
}
/*
void sendString(char *s)
{
while(*s)
{
SendData(*s++);
}
}
*/
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
/*----------------------------
Disable ISP/IAP/EEPROM function
Make MCU in a safe state
----------------------------*/
void IapIdle()
{
IAP_CONTR = 0; //Close IAP function
IAP_CMD = 0; //Clear command to standby
IAP_TRIG = 0; //Clear trigger register
IAP_ADDRH = 0x80; //Data ptr point to non-EEPROM area
IAP_ADDRL = 0; //Clear IAP address to prevent misuse
}
/*----------------------------
Read one byte from ISP/IAP/EEPROM area
Input: addr (ISP/IAP/EEPROM address)
Output:Flash data
----------------------------*/
unsigned char IapReadByte(unsigned int addr)
{
unsigned char dat; //Data buffer
IAP_CONTR = ENABLE_IAP; //Open IAP function, and set wait time
IAP_CMD = CMD_READ; //Set ISP/IAP/EEPROM READ command
IAP_ADDRL = addr; //Set ISP/IAP/EEPROM address low
IAP_ADDRH = addr >> 8; //Set ISP/IAP/EEPROM address high
IAP_TRIG = 0x46; //Send trigger command1 (0x46)
IAP_TRIG = 0xb9; //Send trigger command2 (0xb9)
_nop_(); //MCU will hold here until ISP/IAP/EEPROM operation complete
dat = IAP_DATA; //Read ISP/IAP/EEPROM data
IapIdle(); //Close ISP/IAP/EEPROM function
return dat; //Return Flash data
}
/*----------------------------
Program one byte to ISP/IAP/EEPROM area
Input: addr (ISP/IAP/EEPROM address)
dat (ISP/IAP/EEPROM data)
Output:-
----------------------------*/
void IapProgramByte(unsigned int addr, unsigned char dat)
{
IAP_CONTR = ENABLE_IAP; //Open IAP function, and set wait time
IAP_CMD = CMD_PROGRAM; //Set ISP/IAP/EEPROM PROGRAM command
IAP_ADDRL = addr; //Set ISP/IAP/EEPROM address low
IAP_ADDRH = addr >> 8; //Set ISP/IAP/EEPROM address high
IAP_DATA = dat; //Write ISP/IAP/EEPROM data
IAP_TRIG = 0x46; //Send trigger command1 (0x46)
IAP_TRIG = 0xb9; //Send trigger command2 (0xb9)
_nop_(); //MCU will hold here until ISP/IAP/EEPROM operation complete
IapIdle();
}
/*----------------------------
Erase one sector area
Input: addr (ISP/IAP/EEPROM address)
Output:-
----------------------------*/
void IapEraseSector(unsigned int addr)
{
IAP_CONTR = ENABLE_IAP; //Open IAP function, and set wait time
IAP_CMD = CMD_ERASE; //Set ISP/IAP/EEPROM ERASE command
IAP_ADDRL = addr; //Set ISP/IAP/EEPROM address low
IAP_ADDRH = addr >> 8; //Set ISP/IAP/EEPROM address high
IAP_TRIG = 0x46; //Send trigger command1 (0x46)
IAP_TRIG = 0xb9; //Send trigger command2 (0xb9)
_nop_(); //MCU will hold here until ISP/IAP/EEPROM operation complete
IapIdle();
}
void save_cnt(unsigned int cnt,unsigned int limit)
{
IapEraseSector(0);
Delay10ms();
Delay10ms();
IapProgramByte(0,cnt>>8);
Delay10ms();
IapProgramByte(1,cnt);
Delay10ms();
if(limit!=0&&limit<10000)
{
IapProgramByte(2,limit>>8);
Delay10ms();
IapProgramByte(3,limit);
Delay10ms();
}
}
void get_cnt(unsigned int *cnt,unsigned char addr)
{
*cnt=IapReadByte(addr)<<8;
(*cnt)|=IapReadByte(addr+1);
}
请教高手指点!
|