📄 pyzc1.c
字号:
//#pragma model(196)
#include <80c196kd.h>
#define SP_MODE0 0x00
#define SP_MODE1 0x01
#define SP_MODE2 0x02
#define SP_MODE3 0x03
#define REC_ENABLE 0x08
#define REC_DISABLE 0x00
#define TXD_ENABLE_BIT 0x05
#define EVEN_PARITY 0x08
#define ODD_PARITY 0x28
#define NO_PARITY 0x00
#define SET_BIT_8 0x04
#define SP_INTERNAL_CLK 0x8000
#define SP_EXTERNAL_CLK 0x0000
#define TXD_INTERRUPT 0x00
#define RXD_INTERRUPT 0x01
//#define SERIAL_INT 0x06
#define TI_BIT 0x05
#define RI_BIT 0x06
#define FE_BIT 0x04
#define OE_BIT 0x02
#define RPE_BIT 0x07
#define RB8_BIT 0x07
#define TXE_BIT 0x03
#define T1OVF_DETECTION 2
#define TOVF_INT_MSK 0
#define T2OVF_INT_MSK 4
#define T2CAPTURE_INT_MSK 3
#define T2OVF_DETECTION 3
#define T2_CLOCK_INTERNAL 0
#define TOVF_INT 0
#define AD_INT 1
#define HSI_DATA_AVAIL_INT 2
#define HSO_INT 3
#define HSI0_INT 4
#define SW_TIMER_INT 5
#define SERIAL_INT 6
#define EXT_INT 7
#define TRAP_INT 8
#define UNIMPLEMENTED_INT 9
#define TXD_INT 24
#define RXD_INT 25
#define HSI_FIFO4_INT 26
#define TIMER2_CAPT_INT 27
#define T2OVF_INT 28
#define EXT1_INT 29
#define HSI_FIFO_FULL_INT 30
#define NONMASKABLE_INT 31
/****************************************************************************/
/* */
/* Usefull bit macros. */
/* */
/****************************************************************************/
#define checkbit(var,bit) (var & (0x01 << (bit)))
#define setbit(var,bit) (var |= (0x01 << (bit)))
#define clrbit(var,bit) (var &= (~(0x01 << (bit))))
static unsigned char sp_status_image;
register unsigned int tmpreg;
#pragma interrupt(serial_isr=6)
#pragma interrupt (tovf_isr = TOVF_INT)
const unsigned int ccr = {0x20CD};
#pragma locate(ccr=0x2018)
#define TRANSMIT_BUF_SIZE 20
#define RECEIVE_BUF_SIZE 20
static unsigned char trans_buff[TRANSMIT_BUF_SIZE];
static unsigned char mess[10]={'S',0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,'E'};
static char begin_trans_buff,end_trans_buff;
static unsigned char receive_buff[RECEIVE_BUF_SIZE];
static char end_rec_buff,begin_rec_buff;
static unsigned char timer1_count=0,triggle=0;
extern void enable(void);
void tovf_isr(void)
{
/* User code goes here */
if(++timer1_count==12)
{
triggle=1;
timer1_count=0;
}
}
void transmit(void) /* serial interrupt routine */
{
sp_status_image |= sp_stat; /* image sp_stat into
sp_status_image */
/* transmit a character if there is a character in the buffer
else leave TI_BIT set in image for putchar to enable interrupts */
if(begin_trans_buff!=end_trans_buff)
{
sbuf=trans_buff[begin_trans_buff]; /* transmit character */
/* The next statement makes the buffer circular by starting over when the
index reaches the end of the buffer. */
if(++begin_trans_buff>TRANSMIT_BUF_SIZE - 1)begin_trans_buff=0;
clrbit(sp_status_image,TI_BIT); /* clear TI bit in status_image. */
}
}
int putchar(int c)
{
/* remain in loop while the buffer is full. This is done by checking
the end of buffer index to make sure it does not overrun the
beginning of buffer index. The while instruction checks the case
when the end index is one less then the beginning index and at the
end of the buffer when the beginning index may be equal to 0 and
the end buffer index may be at the buffer end. */
while((end_trans_buff+1==begin_trans_buff)||
(end_trans_buff==TRANSMIT_BUF_SIZE -1 && !begin_trans_buff));
trans_buff[end_trans_buff]=c; /* put character in buffer */
if(++end_trans_buff>TRANSMIT_BUF_SIZE - 1) /* make buffer appear */
end_trans_buff=0; /* circular. */
if(checkbit(sp_status_image, TI_BIT))
{
setbit(int_pend, SERIAL_INT); /* If transmit buffer
was empty, then cause
an interrupt to start
transmitting. */
}
}
void receive(void) /* serial interrupt routine */
{
sp_status_image |= sp_stat; /* image sp_stat into status_image */
/* If the input buffer is full, the last character can be handled
as desired. */
if(end_rec_buff+1==begin_rec_buff || (end_rec_buff==RECEIVE_BUF_SIZE-1 &&
!begin_rec_buff))
{
; /* input overrun code */
}
else
{
/* The next statement makes the buffer circular by starting over when the
index reaches the end of the buffer. */
if(++end_rec_buff > RECEIVE_BUF_SIZE - 1) end_rec_buff=0;
receive_buff[end_rec_buff]=sbuf; /* place character in
buffer */
if(checkbit(sp_status_image, FE_BIT))
{
; /* User code for framing error */
clrbit(sp_status_image, FE_BIT);
}
if(checkbit(sp_status_image, OE_BIT))
{
; /* User code for overrun error */
clrbit(sp_status_image, OE_BIT);
}
}
clrbit(sp_status_image,RI_BIT); /* clear RI bit in status_image. */
}
unsigned char getchar()
{
while(begin_rec_buff==end_rec_buff); /* remain in loop while there is
not a character avaliable. */
if(++begin_rec_buff>RECEIVE_BUF_SIZE - 1) /* make buffer appear */
begin_rec_buff=0; /* circular. */
return(receive_buff[begin_rec_buff]); /* return the character in
buffer. */
}
/* The seperate txd and rxd interrupts can more efficiently process
the interrupts than the generic serial interrupt as this one
does.
*/
void serial_isr(void)
{
sp_status_image |= sp_stat; /* image sp_stat into status_image */
if(checkbit(sp_status_image, RI_BIT))
receive();
else if(checkbit(sp_status_image, TI_BIT))
transmit();
}
void init_serial()
{
/*
* Serial port configuration:
* serial mode = 1
* even parity = disabled
* serial receive = enabled
* serial transmit = enabled
*/
_SetSFR_bit (ioc1, TXD_ENABLE_BIT);
_WriteSFR (sp_con, 0x9);
/*
* Baud Rate = 0
*/
_WriteSFR (baud_rate, 0x70);
_WriteSFR (baud_rate, 0x82);
/*
* Interrupts:
* transmit interrupt = disabled
* receive interrupt = disabled
* serial interrupt = enabled
*/
_SetSFR_bit (int_mask, SERIAL_INT);
_ClrSFR_bit (int_mask1, TXD_INTERRUPT);
_ClrSFR_bit (int_mask1, RXD_INTERRUPT);
end_rec_buff=0; /* initialize buffer pointers */
begin_rec_buff=0;
end_trans_buff=0;
begin_trans_buff=0;
sp_status_image = 0x20; /* Init for initial transmittion */
}
void init_timer1(void)
{
/*
* Timer 1 configuration:
* overflow detection = enabled
*/
_SetSFR_bit (ioc1, T1OVF_DETECTION);
/*
* timer overflow interrupt = enabled
*/
_SetSFR_bit (int_mask, TOVF_INT_MSK);
}
void main(void)
{
unsigned char i;
init_serial();
init_timer1();
enable();
/* The following line will loop until the letter 'Q' is
received. */
putchar('H');
putchar('e');
putchar('l');
putchar('l');
putchar('o');
//while(getchar() != 'Q')
while(1)
{
if(triggle==1)
{
triggle=0;
wsr=0;
mess[6]=ioport0;
mess[7]=ioport2;
for(i=0;i<10;i++) putchar(mess[i]);
}
}
/* Example of sending out buffered data. */
//while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -