📄 st16c55x.c
字号:
#include<msp430x13x.h>
#include"st16c55x.h"
extern void wait_ms(unsigned char i);
unsigned char com1_data[38];
unsigned char com1_i;
unsigned char com2_data[38];
unsigned char com2_i;
unsigned char com3_data[38];
unsigned char com3_i;
unsigned char com4_data[38];
unsigned char com4_i;
void st16c55x_res()
{
DATA_DIR_OUT;
RW_DIR_OUT;
A0_DIR_OUT;
A1_DIR_OUT;
A2_DIR_OUT;
A3_DIR_OUT;
A4_DIR_OUT;
CS_DIR_OUT;
IRQ_DIR_IN;
//RXRDY_DIR_IN;
//TXRDY_DIR_IN;
CS_H;
}
void write_byte(unsigned char dat)
{
DATA_DIR_OUT;
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
DATA_OUT=dat;
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
RW_W;
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
CS_L;
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
CS_H;
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
RW_R;
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
}
unsigned char read_byte()
{
unsigned char i;
DATA_DIR_IN;
_NOP();
_NOP();
_NOP();
RW_R;
_NOP();
_NOP();
_NOP();
CS_L;
_NOP();
_NOP();
_NOP();
_NOP();
i=DATA_IN;
_NOP();
_NOP();
CS_H;
_NOP();
_NOP();
_NOP();
RW_R;
_NOP();
_NOP();
_NOP();
DATA_DIR_OUT;
return(i);
}
/*---------------------------------------------------
串口选择
入口:串口编号,波特率
数据格式默认为 8位数据位,1位停止位,无校验
-----------------------------------------------------*/
void chioce_com(unsigned char port,unsigned long i)
{
switch(port)
{
case 1:CH_COM1;
break;
case 2:CH_COM2;
break;
case 3:CH_COM3;
break;
case 4:CH_COM4;
break;
}
LCR;
write_byte(0x00);
IER;
write_byte(0x00);
LCR;
write_byte(0x80);
switch(i)
{
case 300: DLM;
write_byte(baud_rate[0][1]);
DLL;
write_byte(baud_rate[0][0]);
break;
case 1200: DLM;
write_byte(baud_rate[1][1]);
DLL;
write_byte(baud_rate[1][0]);
break;
case 2400: DLM;
write_byte(baud_rate[2][1]);
DLL;
write_byte(baud_rate[2][0]);
break;
case 9600: DLM;
write_byte(baud_rate[3][1]);
DLL;
write_byte(baud_rate[3][0]);
break;
case 19200: DLM;
write_byte(baud_rate[4][1]);
DLL;
write_byte(baud_rate[4][0]);
break;
case 38400: DLM;
write_byte(baud_rate[5][1]);
DLL;
write_byte(baud_rate[5][0]);
break;
case 56000: DLM;
write_byte(baud_rate[6][1]);
DLL;
write_byte(baud_rate[6][0]);
break;
case 115000: DLM;
write_byte(baud_rate[7][1]);
DLL;
write_byte(baud_rate[7][0]);
break;
}
LCR;
write_byte(0x03); //8位数据位,1个停止位,无校验
FCR;
write_byte(0x07); //使能FIFO,复位发送和接收
IER;
write_byte(0x01); //使能接收,发送中断
}
/*------------------------------------------------
数据发送函数
入口:数据首地址 数据长度
-------------------------------------------------*/
void st16c55x_send(unsigned char *p,unsigned char l,unsigned char port)
{
unsigned char i;
switch(port)
{
case 1:CH_COM1;
break;
case 2:CH_COM2;
break;
case 3:CH_COM3;
break;
case 4:CH_COM4;
break;
}
THR;
for(i=0;i<l;i++)
{
write_byte(*p++);
while((P3IN&0x02)!=0);
}
}
/*----------------------------------------------------------
外部中断初始化函数
-----------------------------------------------------------*/
void P1_Init(void)
{
P1DIR &= 0x7F;
P1IES |= 0x80;
P1IE |= 0x80;
P1IFG=0;
}
/*--------------------------------------------------------------------------
st16c55x中断服务程序
---------------------------------------------------------------------------*/
#pragma vector = PORT1_VECTOR
__interrupt void P1_Isr (void)
{
unsigned char i;
if((P1IFG & 0x80) == 0x80)
{
CH_COM1;
LSR;
i=read_byte();
if((i&0x01)!=0)
{
RHR;
com1_data[com1_i++]=read_byte();
}
CH_COM2;
LSR;
i=read_byte();
if((i&0x01)!=0)
{
RHR;
com2_data[com2_i++]=read_byte();
}
CH_COM3;
LSR;
i=read_byte();
if((i&0x01)!=0)
{
RHR;
com3_data[com3_i++]=read_byte();
}
CH_COM4;
LSR;
i=read_byte();
if((i&0x01)!=0)
{
RHR;
com4_data[com4_i++]=read_byte();
}
}
P1IFG=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -