代码拉取完成,页面将自动刷新
#include "reg51.h"
#include "intrins.h"
#include<absacc.h>
#include<stdio.h>
//sbit TX = P3^1;
sbit EX_WDT = P1^4;
sbit RX_485 = P1^7;
sbit LED = P1^5;
/* define SFR */
sbit ET2 = IE^5;
sfr T2CON = 0xc8; //timer2 control register
sbit TF2 = T2CON^7;
sbit TR2 = T2CON^2;
sbit CT2 = T2CON^1;
sfr T2MOD = 0xc9; //timer2 mode register
sfr RCAP2L = 0xca;
sfr RCAP2H = 0xcb;
sfr TL2 = 0xcc;
sfr TH2 = 0xcd;
#define UART_BUF_SIZE 20
#define wdt_reset() EX_WDT^=1
#define U24_ADDR XBYTE[0x4002]
#define U23_ADDR XBYTE[0x4001]
#define U22_ADDR XBYTE[0x4000]
#define U25_ADDR XBYTE[0x4060]
#define U26_ADDR XBYTE[0x4080]
#define U27_ADDR XBYTE[0x40A0]
#define U28_ADDR XBYTE[0x40C0]
#define U7_ADDR XBYTE[0x8000]
#define BOARD_ADDR XBYTE[0xC000]
#define K4_OFF() do{u24_dat|=0x12;U24_ADDR=u24_dat;}while(0)
#define K4_ON() do{u24_dat&=0xed;U24_ADDR=u24_dat;}while(0)
#define K5_OFF() do{u24_dat|=0x80;U24_ADDR=u24_dat;}while(0)
#define K5_ON() do{u24_dat&=0x7F;U24_ADDR=u24_dat;}while(0)
#define K6_OFF() do{u24_dat|=0x60;U24_ADDR=u24_dat;}while(0)
#define K6_ON() do{u24_dat&=0x9f;U24_ADDR=u24_dat;}while(0)
#define K7_OFF() do{u24_dat|=0x01;u23_dat|=0x80;U24_ADDR=u24_dat;U23_ADDR=u23_dat;}while(0)
#define K7_ON() do{u24_dat&=0xfe;u23_dat&=0x7f;U24_ADDR=u24_dat;U23_ADDR=u23_dat;}while(0)
#define J1_4_L() do{u24_dat|=0x04;U24_ADDR=u24_dat;}while(0)
#define J1_4_H() do{u24_dat&=0xfb;U24_ADDR=u24_dat;}while(0)
#define J1_5_L() do{u24_dat|=0x08;U24_ADDR=u24_dat;}while(0)
#define J1_5_H() do{u24_dat&=0xf7;U24_ADDR=u24_dat;}while(0)
unsigned char uart_rx_cnt=0; //接收到字节数量
unsigned char uart_rx_wp=0; //接收缓存写指针
unsigned char uart_rx_rp=0; //接收缓存读指针
char uart_rx[UART_BUF_SIZE];
unsigned char u23_dat=0xff;
unsigned char u24_dat=0xff;
unsigned char u22_dat=0x00;
unsigned char *ptr;
unsigned int count;
unsigned int tick_1s=0;
volatile char cmd=0;
volatile long frq=0;
//volatile unsigned int frq=0;
//unsigned char xdata tmp[sizeof("STC89-90xx\r\nUart Test !\r\n")]; //在外ram区开辟10个字节的内存空间,地址是外ram的0x0000-0x0009
//code unsigned char tmp[]="STC89-90xx\r\nUart Test !\r\n";//在外ram区开辟10个字节的内存空间,地址是外ram的0x0000-0x0009
//数据个数,JP2-16~JP2-21,VREF,0V3,JP2-29,JP2-30,JP2-32,JP2-34,JP2-27,JP2-25,JP2-24,JP2-26,JP2-28
code unsigned char volt_1x[]= {17,0x80,0x90,0xa0,0xb0,0xc0,0xd0,0xe0,0xf0,0x03,0x02,0x01,0x00,0x40,0x50,0x60,0x70,0x23};
//数据个数,JP2-40,JP2-41,JP2-43
code unsigned char volt_15x[]= {3,0x20,0x21,0x22};
//数据个数,JP2-48,JP2-47|JP2-50,JP2-49|JP2-46,JP2-45|IREF,0V3
code unsigned char cur_50x[]= {4,0x10,0x11,0x12,0x13};
//数据个数,JP2-1~JP2-6,JP2-7~JP2-12
code unsigned char ac_1x[]= {12,0x30,0x31,0x32,0x33,0x34,0x35,0x38,0x39,0x3a,0x3b,0x3c,0x3d};
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 9175000L //System frequency
//#define FOSC 11059200L //System frequency
#define BAUD 9600 //UART baudrate
#define T1MS (65536-FOSC/12/20) //50ms timer calculation method in 12T mode
/*Define UART parity mode*/
#define NONE_PARITY 0 //None parity
#define ODD_PARITY 1 //Odd parity
#define EVEN_PARITY 2 //Even parity
#define MARK_PARITY 3 //Mark parity
#define SPACE_PARITY 4 //Space parity
#define PARITYBIT NONE_PARITY //Testing even parity
//sbit bit9 = P2^2; //P2.2 show UART data bit9
//bit busy;
//void SendData(BYTE dat);
//void SendString(char *s);
char Uart_Getch()
{
char ch=0;
if(uart_rx_cnt > 0)
{
ch = uart_rx[uart_rx_rp];
uart_rx_rp = (uart_rx_rp + 1) % UART_BUF_SIZE;
uart_rx_cnt--;
}
return ch;
}
//#define EX_WDT P1^4
/*--------------------ms延时函数-----------------------*/
/*void delay_ms(unsigned int ms_number) //ms延时函数(AT89C51 @ 11.0592MHz)
{
unsigned int i;
unsigned char j;
for(i=0; i<ms_number; i++)
{
for(j=0; j<200; j++);
for(j=0; j<102; j++);
}
}*/
void t0_init()
{
TMOD |= 0x01; //set timer0 as mode1 (16-bit)
TL0 = T1MS; //initial timer0 low byte
TH0 = T1MS >> 8; //initial timer0 high byte
TR0 = 1; //timer0 start running
ET0 = 1; //enable timer0 interrupt
EA = 1; //open global interrupt switch
count = 0; //initial counter
}
void t2_init()
{
RCAP2L = TL2 = 0; //initial timer2 low byte
RCAP2H = TH2 = 0; //initial timer2 high byte
TR2 = 1; //timer2 start running
CT2=1; //外部计数
//ET2 = 1; //enable timer2 interrupt
//EA = 1; //open global interrupt switch
//count = 0; //initial counter
}
void uart_init()
{
#if (PARITYBIT == NONE_PARITY)
SCON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
SCON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
SCON = 0xd2; //9-bit variable UART, parity bit initial to 0
#endif
TMOD |= 0x20; //Set Timer1 as 8-bit auto reload mode
TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
TR1 = 1; //Timer1 start run
ES = 1; //Enable UART interrupt
EA = 1; //Open master interrupt switch
//TI = 1;
}
void ram_test(int a)
{
int i=0;
unsigned char j=0;
printf("writing ram...\t");
for(i=0; i<=a; i++)
{
XBYTE[i]=(unsigned char)i;
}
printf("write ram OK!\r\n");
printf("reading ram...\t");
for(i=0; i<=a; i++)
{
if(XBYTE[i]!=(unsigned char)i)
{
printf("0x%xfail\t",i);
j++;
if(j>=10)
break;
}
}
if(j)
{
printf("verify ram fail!\r\n");
}
else
{
printf("verify ram succeful!\r\n");
}
}
void get_adc(unsigned char *p)
{
unsigned char i=0;
for(i=1; i<=p[0]; i++)
{
U22_ADDR=u22_dat=p[i];
count=0;
while(count<2)
wdt_reset();
switch(p[0])
{
case 17:
// printf("0x%x\tfrq:%d\tvolt:%d\r\n",(int)u22_dat,(int)frq,(int)((frq-3731)*13/10));
printf("%dmV\t",(int)((frq-3731)*13/10));
break;
case 3:
//printf("0x%x\tfrq:%d\tvolt:%d\r\n",(int)u22_dat,(int)frq,(int)(frq-3731)/763);
printf("%dmV\t",(int)((frq-3731)*20));
break;
case 4:
//printf("0x%x\tfrq:%d\tvolt:%d\r\n",(int)u22_dat,(int)frq,(int)(frq-3731)/763);
printf("%dmV\t",(int)((frq-3731)*13/10));
break;
case 12:
//printf("0x%x\tfrq:%d\tvolt:%d\r\n",(int)u22_dat,(int)frq,(int)(frq-3731)/763);
printf("%dmV\t",(int)((frq-3731)*13/10+1394));
break;
}
}
printf("\r\n");
}
void menu()
{
printf("\n###### L8配电监控板测试 ######\r\n");
printf(" [0] 测试K4\r\n");
printf(" [1] 测试K5\r\n");
printf(" [2] 测试K6\r\n");
// Uart_Printf(" [a] Write File to NOR Flash\n");
printf(" [3] 测试K7\r\n");
printf(" [4] 测试J1-4(需加上拉电阻)\r\n");
printf(" [5] 测试J1-5(需加上拉电阻)\r\n");
printf(" [6] 外部RAM测试[0x0000-0x3fff]\r\n");
// printf(" [7] 外部RAM测试[0x0000-0x4000]\r\n");
printf(" [7] 读取板地址\r\n");
printf(" [8] 读取U25 U26 U27 U28 U7 P3.0-3.7状态\r\n");
printf(" [9] 1倍电压检测(-4~8V)\r\n"); //OK
printf(" [a] 15倍缩小电压检测\r\n");//OK
//printf(" [b] 15倍缩小电压检测\r\n");
printf(" [b] 电流(差分50倍放大)检测\r\n");
printf(" [c] 带整流电压检测(-8~8V)\r\n"); //OK
printf(" [h] 显示菜单\r\n");
}
void main(void)
{
//tmp[0]='a';
//tmp[1]='b';
//tmp[2]='c';
//tmp[3]=0;
// unsigned char dat;
// char a,b;
char key=0;
uart_init();
t0_init();
t2_init();
//SendString(tmp);
// printf("你好%d\r\n",(int)123);
//U24_ADDR=0x80;
//P0=0;
menu();
//U22_ADDR=0xe0; //vref
while(1)
{
wdt_reset();
key=Uart_Getch();
switch(key)
{
case '0':
if((u24_dat&0x12)==0)
{
K4_OFF();
printf("K4 OFF\r\n");
}
else
{
K4_ON();
printf("K4 ON\r\n");
}
break;
case '1':
if((u24_dat&0x80)==0)
{
K5_OFF();
printf("K5 OFF\r\n");
}
else
{
K5_ON();
printf("K5 ON\r\n");
}
break;
case '2':
if((u24_dat&0x60)==0)
{
K6_OFF();
printf("K6 OFF\r\n");
}
else
{
K6_ON();
printf("K6 ON\r\n");
}
break;
case '3':
if(!(u24_dat&0x01)&&!(u23_dat&0x80))
{
K7_OFF();
printf("K7 OFF\r\n");
}
else
{
K7_ON();
printf("K7 ON\r\n");
}
break;
case '4':
if((u24_dat&0x04)==0)
{
J1_4_L();
printf("J1-4 OFF\r\n");
}
else
{
J1_4_H();
printf("J1-4 ON\r\n");
}
break;
case '5':
if((u24_dat&0x08)==0)
{
J1_5_L();
printf("J1-5 OFF\r\n");
}
else
{
J1_5_H();
printf("J1-5 ON\r\n");
}
break;
case '6':
ram_test(0x3fff);
//ram_test(0x4000);
break;
case '7':
printf("板地址:0x%x\r\n",(int)BOARD_ADDR);
break;
case '8':
printf("u25:%x\tu26:%x\tu27:%x\tu28:%x\tu7:%x\tP3.3-3.5:%x\r\n",(int)U25_ADDR,(int)U26_ADDR,(int)U27_ADDR,(int)U28_ADDR,(int)U7_ADDR,(int)P3&0x38);
break;
case '9':
printf("JP2-16\tJP2-17\tJP2-18\tJP2-19\tJP2-20\tJP2-21\tVREF\t0V3\tJP2-29\tJP2-30\tJP2-32\tJP2-34\tJP2-27\tJP2-25\tJP2-24\tJP2-26\tJP2-28\r\n");
get_adc(volt_1x);
// adc();
// scanf("%x",&u22_dat);
// printf("\n%x\n",u22_dat);
//U22_ADDR=u22_dat=0xe0; //vref
break;
case 'a':
printf("JP2-40\tJP2-41\tJP2-43\r\n");
get_adc(volt_15x);
//U22_ADDR=u22_dat=0x21; //BV-1(JP2-41)
break;
case 'b':
printf("JP2-48,JP2-47\tJP2-50,JP2-49\tJP2-46,JP2-45\tIREF,0V3\r\n");
get_adc(cur_50x);
//U22_ADDR=u22_dat=0x00; //JP2-34
break;
case 'c':
printf("JP2-1\tJP2-2\tJP2-3\tJP2-4\tJP2-5\tJP2-6\tJP2-7\tJP2-8\tJP2-9\tJP2-10\tJP2-11\tJP2-12\r\n");
get_adc(ac_1x);
//U22_ADDR=u22_dat=0x30; //JP2-1
break;
case 'h':
menu();
break;
default:
break;
}
// printf("u23:%x\tu24:%x\r\n",(int)u23_dat,(int)u24_dat);
// TX^=1;
// U23_ADDR=0xff;
// (*(volatile char *))
// LED^=1;
//delay_ms(500);
//EX_WDT=1;
//U23_ADDR=0x80;
// delay_ms(500);
// dat=XBYTE[0xC000];
// SendData(dat);
//SendData(0x55);
}
}
/* Timer0 interrupt routine */
void tm0_isr() interrupt 1 using 1
{
frq=TL2+((unsigned int)TH2<<8);
TL2=0;
TH2=0;
TL0 = T1MS; //reload timer0 low byte
TH0 = T1MS >> 8; //reload timer0 high byte
count++;
/* if(count>=20)
{
count=0;
tick_1s=1;
LED = ! LED; //work LED flash
}*/
}
/* Timer2 interrupt routine */
/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4 using 1
{
if (RI)
{
RI = 0; //Clear receive interrupt flag
// P0 = SBUF; //P0 show UART data
// bit9 = RB8; //P2.2 show parity bit
//LED^=1;
if(uart_rx_cnt < UART_BUF_SIZE)
{
uart_rx[uart_rx_wp] = SBUF;
uart_rx_wp = (uart_rx_wp + 1) % UART_BUF_SIZE;
uart_rx_cnt++;
}
}
// if (TI)
// {
//RX_485=1;
// TI = 0; //Clear transmit interrupt flag
// busy = 0; //Clear transmit busy flag
// }
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
//void SendData(BYTE dat)
char putchar (char c)
{
RX_485=0;
// //Wait for the completion of the previous data is sent
TI = 0;
SBUF = c;
while(!TI);
TI = 0;
RX_485=1;
return 0;
}
/*char hal_uart_getchar(void)
{
unsigned char ch;
//Wait until a character is available:
while(uart_rx_cnt == 0)
wdt_reset();
ES = 0;
ch = uart_rx[uart_rx_rp];
uart_rx_rp = (uart_rx_rp + 1) % UART_BUF_SIZE;
uart_rx_cnt--;
ES = 1;
return ch;
}
char _getkey (void)
{
return hal_uart_getchar();
}*/
/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
//void SendString(char *s)
//{
// while (*s) //Check the end of the string
// {
// putchar(*s++); //Send current char and increment string ptr
// }
//}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。