main.c
#include <stc8a8k.h>
#include <stdio.h>
#include "intrins.h"
#include "disp.h"
#define FOSC 11059200UL
#define BRT (65536 - FOSC / 115200 / 4)
sbit LED=P1^7;
sbit PUL0=P0^0;
int count5ms=6;
int pwmfreq=100;
int pulsenum=560;
char dir=1;
char buffer[16];
void UartInit(void);
void printf_char_int_long(void);
void printf_float(void);
void printf_String(void);
void UART1_SendData(char dat);
void UartSendStr(char *p);
char putchar(char c);
void Delay800ms();
void Delay10ms()
{
unsigned char i, j;
_nop_();
_nop_();
i = 144;
j = 157;
do
{
while (--j);
} while (--i);
}
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x00;
TH0 = 0x28;
TF0 = 0;
TR0 = 0;
}
void TM0_Isr() interrupt 1 using 1
{
static int cnt5ms=0;
if(cnt5ms>count5ms)
{
cnt5ms=0;
PUL0 = !PUL0;
}
else
cnt5ms++;
}
void main()
{
unsigned char data0=0x10;
UartInit();
ES = 1;
EA = 1;
Timer0Init();
TR0 = 0;
ET0 = 0;
while(1)
{
}
}
void UART1_SendData(char dat)
{
ES=0;
SBUF=dat;
while(TI!=1);
TI=0;
ES=1;
}
void UartSendStr(char *p)
{
while (*p)
{
UART1_SendData(*p++);
}
}
char putchar(char c)
{
UART1_SendData(c);
return c;
}
void Delay800ms()
{
unsigned char i, j, k;
i = 45;
j = 100;
k = 200;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void UartInit(void)
{
SCON = 0x50;
AUXR |= 0x01;
AUXR &= 0xFB;
T2L = 0xFE;
T2H = 0xFF;
AUXR |= 0x10;
}
void UartIsr() interrupt 4 using 1
{
unsigned char cmdtemp=0;
if (TI)
{
TI = 0;
}
if (RI)
{
cmdtemp=SBUF;
switch(cmdtemp)
{
case 'q':
pwmfreq=pwmfreq+10;
count5ms=count5ms+5;
disp_gotoxy(1,2);
printf("count5ms=%d\r\n",count5ms);
break;
case 'w':
pwmfreq=pwmfreq-10;
count5ms=count5ms-5;
disp_gotoxy(1,2);
printf("count5ms=%d\r\n",count5ms);
break;
case 'e':
LED=0;
TR0 = 0;
ET0 = 0;
break;
case 'r':
LED=1;
TR0 = 1;
ET0 = 1;
break;
default:break;
}
RI = 0;
}
}
void printf_char_int_long(void)
{
char a=-100;
int b=-2000;
long c=6553600;
printf ("char %bd int %d long %ld\r\n",a,b,c);
printf ("char_0x%bx int_0x%x long_0x%lx\r\n",a,b,c);
printf ("a_int %d\r\n",(int)(a));
printf ("char %bd,int %d,long %ld\r\n",a,b,c);
}
void printf_float(void)
{
float a= 3.14159265798932;
float f = 10.0,g = 22.95;
printf("Max is %f\r\n",a);
printf("%.4f\r\n",a);
printf("%.12f\r\n",a);
printf ("%f , %g\r\n", f, g);
}
void printf_String(void)
{
char buf [] = "Test String";
char *p = buf;
printf ("String %s is at address %p\r\n",buf,p);
}
disp.h
#ifndef __DISP_H
#define __DISP_H
void disp_clean(void);
void disp_cursor_reset(void);
void disp_gotoxy(int x,int y);
void disp_cursor_up(int y);
void disp_cursor_down(int y);
void disp_cursor_left(int x);
void disp_cursor_right(int x);
void disp_cursor_hide(void);
void disp_cursor_show(void);
#endif
disp.c
#include "disp.h"
#include "stdio.h"
void disp_clean(void)
{
printf("\033[2J");
}
void disp_cursor_reset(void)
{
printf("\033[H");
}
void disp_gotoxy(int x,int y)
{
printf("\033[%d;%dH",y,x);
}
void disp_cursor_up(int y)
{
printf("\033[%dA",y);
}
void disp_cursor_down(int y)
{
printf("\033[%dB",y);
}
void disp_cursor_left(int x)
{
printf("\033[%dD",x);
}
void disp_cursor_right(int x)
{
printf("\033[%dC",x);
}
void disp_cursor_hide(void)
{
printf("\033[?25l");
}
void disp_cursor_show(void)
{
printf("\033[?25h");
}