⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 16c554.txt

📁 并口扩4串口的程序
💻 TXT
📖 第 1 页 / 共 2 页
字号:
#include  <MSP430X14X.H>    

#include  <MATH.H>    

   

#define  LSB          0x00           //除数锁存器低位    

#define  MSB          0x04           //除数锁存器高位    

   

#define  RHR          0x00           //16C554寄存器选择,P1.2-p1.4与A0-A2相连    

#define  THR          0x00    

#define  IER          0x04    

#define  EFR          0x08    

#define  FCR          0x08    

#define  ISR          0x08    

#define  LCR          0x0c    

#define  MCR          0x10    

#define  LSR          0x14    

#define  MSR          0x18    

   

#define  WRITE_EN     0x02            //写信号选择,p1.1    

#define  READ_EN      0x01            //读信号选择,p1.0    

   

#define  CSA          0x0e0           //A口的片选信号,p1.5-p1.7    

#define  CSB          0x0c0           //B口的片选信号    

#define  CSC          0x0a0    

#define  CSD          0x80    

   

#define    num1 20    

#define    num2 18    

#define    num3 12    

#define    num4 3    

#define    num5 3    

#define    num6 5    

   

unsigned char M_com_initf[num1]  = {'a','t','b','0','e','0','q','1','p','s','0','=','1','&','y','1','&','w','1',13};    //q1状态码不送回终端机,&y1自动载入预存第1组参数值,&w1将现行参数存入第1组存储器,s0=1表示自动接听,最后一个是回车,modem的初始化命令    

   

unsigned char M_com_retry[num4]  = {'a','/',13};                           //重拨命令    

unsigned char M_com_escape[num5] = {'+','+','+'};                          //数据模式跳到指令模式    

unsigned char M_com_end[num6]    = {'a','t','h','0',13};                   //挂机命令    

   

#define con_port        P1OUT                //16c554的控制端口    

#define con_port_dir    P1DIR                //控制端口的方向选择    

#define data_out_port   P4OUT                //数据端口输出    

#define data_in_port    P4IN                 //数据端口输入    

#define data_port_dir   P4DIR                //数据端口的方向选择    

   

#define con_leisure     0x0e3                //p1口为16c554控制信号,读写信号禁止(0:1),A0-A2为000(2:3:4),片选为一个无用的端口(5:6:7)    

#define interrupt_edge  P2IES                // Port 2 Interrupt Edge Select     

#define interrupt_en    P2IE                 // Port 2 Interrupt Enable     

#define interrupt_flag  P2IFG                //中断标志    

   

#define st16c554_mhz    1.8432               //16c554外接的晶振频率,单位(MHZ)    

#define dl_554          (st16c554_mhz*1000000/16)                 //分频系数计算的中间常量    

   

#define  data_maxa        23                                      //一个数据包的最大字节数    

#define  buf_len_554     1*data_maxa                             //数组的长度,3表示可以存3个数据包    

unsigned char Rx_flag_554[4];                                     //Rx_flag_554表示接收到的数据包的个数    

unsigned char Rx_out_554[4];                                      //缓冲数组的读数据指针    

   

extern unsigned char PSTN_num[16];   

extern unsigned char rxdata[6][25];   

extern unsigned char port554_r;   

void low_power(void);   

//extern void command_oper(unsigned char uartn);    

//extern void digit_sensor(unsigned char uartn);    

   

//A口    

unsigned char Rx_in_554a;                                         //接收缓冲数组的存数据指针    

unsigned char Rx_full_554a;                                       //接收数据满    

unsigned char Rx_554a_start;                                      //Rx_554a_start表示开始接收有效的数据,    

unsigned char data_num_554a;                                      //接收到的数据的字节数    

unsigned char checksuma;                                          //校验和    

unsigned char rx_temp1_554a,rx_temp2_554a;                        //接收数据的临时存储单元    

   

//B口    

#define  data_maxb        23    

unsigned char Rx_in_554b;   

unsigned char Rx_full_554b;                                       //接收数据满    

unsigned char Rx_554b_start;   

unsigned char data_num_554b;   

unsigned char checksumb;                                          //校验和    

unsigned char rx_temp1_554b,rx_temp2_554b;   

   

//C口    

#define  data_maxc        23    

unsigned char Rx_in_554c;   

unsigned char Rx_full_554c;   

unsigned char Rx_554c_start;   

unsigned char data_num_554c;   

unsigned char checksumc;                                          //校验和    

unsigned char rx_temp1_554c,rx_temp2_554c,rx_temp3_554c;   

   

//D口       ,连接7字节协议数字传感器    

#define  data_maxd        7    

unsigned char Rx_in_554d;   

unsigned char Rx_full_554d;   

unsigned char Rx_554d_start;   

unsigned char data_num_554d;   

unsigned char checksumd;                                          //校验和    

unsigned char rx_temp1_554d,rx_temp2_554d;   

   

unsigned char DCD_flag,CTS_flag;                                  //C口的载波信号和清除发送信号的标志    

   

   

   

   

   

/****************************************************  

*  

*    名称:delayus()  

*    功能:延时时间= (5*i+5)*指令周期(us)  

*    入口参数:i 为循环次数  

*  

****************************************************/   

   

void delayus(unsigned int i)   

{   

    while(--i);   

}   

   

/****************************************************  

*  

*    名称:delayms()  

*    功能:延时时间= i(ms)  

*    入口参数:i 为循环次数  

*  

****************************************************/   

   

void delayms(unsigned int i)   

{   

    while(--i)   

    {   

        delayus(200);   

    }   

}   

   

void delays(unsigned int i)   

{   

    while(--i)   

    {   

        delayms(1000);   

    }   

}   

   

/****************************************************  

*  

*    名称:init_16c554a()  

*    功能:对16c554的a口初始化  

*    入口参数:select_554IO是16C554的端口选择:0-A口,1-B口,3-D口;bps是波特率  

*    出口:无  

*  

****************************************************/   

   

void init_16c554port(unsigned char select_554IO,unsigned int bps)   

{   

    unsigned int divisor;            //分频系数    

    unsigned char CS;                //片选信号    

    if(select_554IO == 0)            //判断是选择哪个口,然后把该口的片选信号给CS    

        CS = CSA;   

    else if(select_554IO == 1)   

             CS = CSB;   

         else if(select_554IO == 3)   

                  CS = CSD;   

              else return;   

    divisor = dl_554/bps;   

    con_port_dir = 0x0ff;            //16c554控制信号为输出    

    con_port = con_leisure;          //读写信号禁止,A0-A2为000,片选为一个无用的端口    

    data_port_dir = 0x0ff;           //16c554数据信号为输出    

       

    con_port |= LCR;                 //选中16c554的LCR寄存器    

    con_port &= ~CS;                 //片选端口    

    con_port ^= WRITE_EN;            //选中寄存器的写    

    data_out_port = 0x83;            //使能波特率设置    

    con_port = con_leisure;   

       

    con_port |= LSB;                 //选中16C554波特率设置寄存器的低位    

    con_port &= ~CS;   

    con_port ^= WRITE_EN;            //选中寄存器的写    

    data_out_port = divisor%256;     //设置通讯波特率为9600    

    con_port = con_leisure;   

       

    con_port |= MSB;                 //选中16C554波特率设置寄存器的高位    

    con_port &= ~CS;   

    con_port ^= WRITE_EN;            //选中寄存器的写    

    data_out_port = divisor/256;   

    con_port = con_leisure;   

       

    con_port |= FCR;                 //FCR寄存器    

    con_port &= ~CS;   

    con_port ^= WRITE_EN;            //选中寄存器的写    

    data_out_port = 0x06;            //FIFO模式    

    con_port = con_leisure;   

       

    con_port |= LCR;                 //LCR    

    con_port &= ~CS;   

    con_port ^= WRITE_EN;                

    data_out_port = 0x03;            //8 bit_data, 1 bit_stop, no parity    

    con_port = con_leisure;   

       

    con_port |= IER;   

    con_port &= ~CS;   

    con_port ^= WRITE_EN;            //IER    

    data_out_port = 0x11;            //接收中断   ,IER-bit4=1,sllep    

    con_port = con_leisure;   

       

    con_port |= MCR;                 //MCR    

    con_port &= ~CS;   

    con_port ^= WRITE_EN;   

    data_out_port = 0x08;            //禁止回写模式    

    con_port = con_leisure;   

    low_power();   

}   

   

/****************************************************  

*  

*    名称:init_16c554c()  

*    功能:对16c554的c口初始化  

*    入口参数:bps 波特率  

*    出口:无  

*  

****************************************************/   

   

void init_16c554c(unsigned int bps)   

{   

    unsigned int divisor;            //分频系数    

    divisor = dl_554/bps;   

       

    con_port_dir = 0x0ff;            //16c554控制信号为输出    

    con_port = con_leisure;          //读写信号禁止,A0-A2为000,片选为一个无用的断口    

    data_port_dir = 0x0ff;           //16c554数据信号为输出    

       

    con_port |= LCR;                //选中16c554的LCR寄存器    

    con_port &= ~CSC;               //片选中C口    

    con_port ^= WRITE_EN;           //选中寄存器的写    

    data_out_port = 0x83;           //使能波特率设置    

    con_port = con_leisure;   

       

    con_port |= LSB;                //选中16C554波特率设置寄存器的低位    

    con_port &= ~CSC;               //片选C口    

    con_port ^= WRITE_EN;           //选中寄存器的写    

    data_out_port = divisor%256;    //设置通讯波特率    

    con_port = con_leisure;   

       

    con_port |= MSB;               //选中16C554波特率设置寄存器的高位    

    con_port &= ~CSC;              //片选C口    

    con_port ^= WRITE_EN;          //选中寄存器的写    

    data_out_port = divisor/256;   

    con_port = con_leisure;   

       

    con_port |= FCR;               //FCR寄存器    

    con_port &= ~CSC;              //片选C口    

    con_port ^= WRITE_EN;          //选中寄存器的写    

    data_out_port = 0x06;          //FIFO模式    

    con_port = con_leisure;   

       

    con_port |= LCR;               //LCR    

    con_port &= ~CSC;   

    con_port ^= WRITE_EN;                

    data_out_port = 0x03;          //8 bit_data, 1 bit_stop, no parity    

    con_port = con_leisure;   

       

    con_port |= IER;   

    con_port &= ~CSC;   

    con_port ^= WRITE_EN;          //IER    

    data_out_port = 0x19;          // modem中断和接收中断,, ,IER-bit4--sleep    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -