#define GPX2CON (*(volatile unsigned int *)0x11000c40) //LED2
#define GPX2DA (*(volatile unsigned int *)0x11000c44)
#define GPX1CON (*(volatile unsigned int *)0x11000c20) //LED3
#define GPX1DA (*(volatile unsigned int *)0x11000c24)
#define GPF3CON (*(volatile unsigned int *)0x114001e0) //LED4 5
#define GPF3DA (*(volatile unsigned int *)0x114001e4)
#define GPA1CON (*(volatile unsigned int *)0x11400020)
#define ULCON2 (*(volatile unsigned int *)0x13820000)
#define UCON2 (*(volatile unsigned int *)0x13820004)
#define UTRSTAT2 (*(volatile unsigned int *)0x13820010)
#define UTXH2 (*(volatile unsigned int *)0x13820020)
#define URXH2 (*(volatile unsigned int *)0x13820024)
#define UBRDIV2 (*(volatile unsigned int *)0x13820028)
#define UFRACVAL2 (*(volatile unsigned int *)0x1382002c)
#define ADCMUX (*(volatile unsigned int *)0x126c001c) //通道选择寄存器
#define ADCDAT (*(volatile unsigned int *)0x126c000c) //adc数据寄存器
#define ADCCON (*(volatile unsigned int *)0x126c0000) //adc控制寄存器
#define GPDCON0 (*(volatile unsigned int *)0x114000a0) //脉冲输出
#define TCFG0 (*(volatile unsigned int *)0x139d0000) //一级分频
#define TCFG1 (*(volatile unsigned int *)0x139d0004) //二级分频
#define TCNTB0 (*(volatile unsigned int *)0x139d000c) //时钟计数器
#define TCMPB0 (*(volatile unsigned int *)0x139d0010) //时钟比较器
#define TCON (*(volatile unsigned int *)0x139d0008) //pwm控制器
#define WTCON (*(volatile unsigned int *)0x10060000) //看门沟控制寄存器
#define WTCNT (*(volatile unsigned int *)0x10060008)//看门狗计数器
//bit15--bit8 ===== 249, 一级分频250
//bit5=1, 使能看门狗
//bit4-bit3 = 3, 二级分频 128
//bit2=0, 看门狗时间到, 不产生中断, reset
//bit0 = 1, 时间到, 直接reset
//============== 31250
//看门狗 工作时钟 = 100m/250/128 ========3125
//需要10秒 的看门狗, 10 / (1/3125) =========31250
void wdtInit()
{
WTCNT = 21875;
WTCON = WTCON &~(0xff<<8);
WTCON = WTCON | (249<<8);
WTCON = WTCON | (3<<3);
WTCON = WTCON &~(1<<2);
WTCON = WTCON |(1<<0);
WTCON = WTCON |(1<<5);
}
void feedDog()
{
WTCNT = 21875;
}
void pwmInit()
{
GPDCON0 = GPDCON0 &~(0xf);
GPDCON0 = GPDCON0 |(0x2);
TCFG0 = TCFG0 &~(0xffff);
TCFG0 = TCFG0 | (249);
TCFG1 = TCFG1 &~(0xf);
TCFG1 = TCFG1 |(3);
TCNTB0 = TCNTB0 &(0);
TCNTB0 = TCNTB0 | (250);
TCMPB0 = TCMPB0 &(0);
TCMPB0 = TCMPB0 |(75);
TCON = TCON &~(0xf);
TCON = TCON | (1<<3);
TCON = TCON |(1<<1);
TCON = TCON &~(1<<1);
}
void adcInit()
{
ADCMUX = ADCMUX & ~(0xf);
ADCMUX = 3;
ADCCON = ADCCON & ~(0x1bfc7);
ADCCON = ADCCON & ~(0X3<<1);
ADCCON = ADCCON | (0x13<<6);
ADCCON = ADCCON & ~(1<<16);
ADCCON = ADCCON | (1<<14);
}
void uartSend(char buf);
int readAdc()
{
int x;
ADCCON = ADCCON | 1;
while((ADCCON & (1<<15))==0);
x = ADCDAT & 0x3ff;
return x;
}
void uartInit()
{
GPA1CON = GPA1CON & ~(0Xff);
GPA1CON = GPA1CON | 0X22;
ULCON2 = ULCON2 & ~(0x63);
ULCON2 = ULCON2 | 0x3;
ULCON2 = ULCON2 & ~(0X6<<4);
UCON2 = UCON2 & ~(0xf);
UCON2 = UCON2 | (0xf);
UBRDIV2 = 53;
UFRACVAL2 = 4;
}
void uartSend(char buf)
{
while((UTRSTAT2 & (1<<1))==0);
UTXH2 = buf;
}
char uartRecv()
{
char x ;
while((UTRSTAT2 & (1<<0))==0);
x = URXH2;
return x;
}
char nowaitRecv()
{
char buf;
while(UTRSTAT2 &(1<<0)==0);
buf = URXH2;
return buf;
}
void led_init()
{
GPX2CON = GPX2CON & ~(0xf<<4*7);
GPX2CON = GPX2CON | (1<<4*7);
GPX1CON = GPX1CON & ~(0xf<<4*0);
GPX1CON = GPX1CON | (1<<4*0);
GPF3CON = GPF3CON & ~(0xff<<2*8);
GPF3CON = GPF3CON | (0x11<<2*8);
}
void led2_on()
{
GPX2DA = GPX2DA | (1<<7);
}
void led3_on()
{
GPX1DA = GPX1DA | (1<<0);
}
void led4_on()
{
GPF3DA = GPF3DA | (1<<4);
}
void led5_on()
{
GPF3DA = GPF3DA | (1<<5);
}
void led2_off()
{
GPX2DA = GPX2DA & ~(1<<7);
}
void led3_off()
{
GPX1DA = GPX1DA & ~(1<<0);
}
void led4_off()
{
GPF3DA = GPF3DA & ~(1<<4);
}
void led5_off()
{
GPF3DA = GPF3DA & ~(1<<5);
}
void mydelay()
{
int i ;
for(i = 0;i<1000000;i++);
}
void A_moudle()
{
int j = 0;
char buf;
while(1)
{
buf = nowaitRecv();
if(buf == 'q')
{
uartSend('q');
break;
}
if(j==4)
{
j=0;
}
if(j==0)
{
led2_on();
mydelay();
led2_off();
}
else if (j==1)
{
led3_on();
mydelay();
led3_off();
}
else if (j==2)
{
led4_on();
mydelay();
led4_off();
}
else if (j==3)
{
led5_on();
mydelay();
led5_off();
}
mydelay();
j++;
}
}
void B_moudle()
{
int i = 0;
char buf;
while(1)
{
buf = nowaitRecv();
if(buf == 'q')
{
break;
}
if(i==2)
{
i=0;
}
if(i==0)
{
led2_on();
led5_on();
mydelay();
led2_off();
led5_off();
}
else if(i==1)
{
led3_on();
led4_on();
mydelay();
led3_off();
led4_off();
}
mydelay();
i++;
}
}
void C_moudle()
{
char buf;
while(1)
{
buf = nowaitRecv();
if(buf =='q')
{
break;
}
led2_on();
led3_on();
led4_on();
led5_on();
mydelay();
led2_off();
led3_off();
led4_off();
led5_off();
mydelay();
}
}
void bz_delay()
{
int i = 0;
while(i++<9999);
}
void bz_play()
{
int i= 10;
char buf;
while(i++<=230)
{
buf = uartRecv();
if(buf == 's')
{
uartSend('s');
TCON = TCON &~(1);
break;
}
if(i==230)
{
i=10;
}
TCMPB0 = i;
TCON = TCON|(1<<1);
TCON = TCON&~(1<<1);
TCON = TCON|(1);
bz_delay();
TCON = TCON&~(1);
}
}
int main()
{
int x;
char q_x ;
char b_x ;
char s_x ;
char g_x ;
char buf;
led_init();
uartInit();
adcInit();
pwmInit();
wdtInit();
while(1)
{
feedDog();
buf = uartRecv();
if(buf == 'a')
{
uartSend('a'+2);
A_moudle();
}
if(buf == 'b')
{
uartSend('b'+2);
B_moudle();
}
if(buf == 'c')
{
uartSend('c'+2);
C_moudle();
}
if(buf == 'o')
{
uartSend('\r');
uartSend('\n');
x=readAdc();
ADCCON = ADCCON & ~(1<<15);
g_x = x%10+48;
s_x = x/10%10+48;
b_x = x/100%10+48;
q_x=x/1000%10+48;
if(q_x != '0')
{
uartSend(q_x);
uartSend(b_x);
uartSend(s_x);
uartSend(g_x);
continue;
}
if(b_x!='0')
{
uartSend(b_x);
uartSend(s_x);
uartSend(g_x);
continue;
}
if(s_x!='0')
{
uartSend(s_x);
uartSend(g_x);
continue;
}
uartSend(g_x);
}
if(buf == 'p')
{
uartSend('p');
bz_play();
}
if(buf == 'e')
{
uartSend('e');
led2_off();
led3_off();
led4_off();
led5_off();
}
}
}