通用GPIO模拟串口,提供源代码,本人经过测试OK(第一版)

--------------------------serial.h------------------------------------------
#ifndef _SERIAL_H_
#define _SERIAL_H_
#define my_board
#ifdef my_board //只需修改这里,就可移植到STM32等多种支持定时器的单片机中。
void rx_timer_inter(void);
void tx_timer_inter(void);
void init_timer(void);
void start_tx_timer(void);//接收和发送分别采用一个定时器
void start_rx_timer(void);
void stop_rx_timer(void);
void stop_rx_timer(void);
//GPIO模块串口TX
//GPIO模拟串口RX
void init_gpio(void); //配置TX为输出,RX为输入管脚,并设置TX管脚的初始状态为1
unsigned char read_rx_line(void);
void write_tx_line(unsigned char dat);
#endif
//串口信息的结构体
struct serial_t{
 unsigned char digital;
 unsigned char parriy;  //none even odd
 unsigned char stop;
 unsigned char baud;
};
static struct serial_t serial;
; //定时器修改此标志
; //
unsigned char make_odd_bit(unsigned char ch);
unsigned char make_even_bit(unsigned char ch);
unsigned char odd_check(unsigned char ch,unsigned char check);
unsigned char even_check(unsigned char ch,unsigned char check);
void serial_init(void);
unsigned char recv_one_bit(void);
unsigned char is_start_bit(void);
unsigned char is_stop_bit(void);
unsigned char recv_one_byte(unsigned char *ch);
unsigned char recv_n_byte(unsigned char *buf);
void send_one_byte(unsigned char ch);
void send_n_byte(unsigned char *buf,unsigned char num);
#endif
------------------------------------------------------------------
----------------------------serial.c--------------------------------
#include "serial.h"
#include "global.h"
#ifdef my_board
void rx_timer_inter(void)//中断服务程序
{
 rx_flag = ;
}
void tx_timer_inter(void)//中断服务程序
{
 tx_flag = ;
}
void init_timer(void)
{
    STR_FP_ISR_CALLBACK fp;
    fp = GetOldIsrCallback();
    fp.Timer0 = tx_timer_inter;
    RegIsrCallback(fp);
    TimerInit(TIMER0,,,INT);
    fp = GetOldIsrCallback();
    fp.Timer1 = rx_timer_inter;
    RegIsrCallback(fp);
    TimerInit(TIMER1,,,INT);
}
void start_tx_timer(void)
{
    StartTimer(TIMER0);
    tx_flag = ;
}
void start_rx_timer(void)
{
    StartTimer(TIMER1);
    rx_flag = ;
}
void stop_tx_timer(void)
{
    StopTimer(TIMER0);
}
void stop_rx_timer(void)
{
    StopTimer(TIMER1);
}
//GPIO模块串口TX
//GPIO模拟串口RX
void init_gpio(void)
{
    GpioInit(GPIOC,,OUTPUT);
    GpioBitOut(GPIOC,,);
    GpioInit(GPIOC,,INPUT);
}
unsigned char read_rx_line(void)
{
    );
}
void write_tx_line(unsigned char dat)
{
 GpioBitOut(GPIOC,,dat);
}
void serial_init(void)
{
 serial.digital = ;
 serial.stop = ;
 serial.parriy = ;
 serial.baud = ; // BAUD = baud / 9600
 init_timer();
 init_gpio();
}
#endif
unsigned char make_odd_bit(unsigned char ch)
{
 unsigned char check,i;
 ;i<;i++)
 {
  check +=  ( (ch & (<<i)) >> i );
 }
  == )
  ;
 else
  ;
}
unsigned char make_even_bit(unsigned char ch)
{
   unsigned char check,i;
 ;i<;i++)
 {
  check +=  ( (ch & (<<i)) >> i );
 }
  == )
  ;
 else
  ;
}
unsigned char odd_check(unsigned char ch,unsigned char check)
{
 unsigned char temp;
 temp = make_odd_bit(ch);
 if(temp == check)
  ;
 else
  ;
}
unsigned char even_check(unsigned char ch,unsigned char check)
{
 unsigned char temp;
 temp = make_even_bit(ch);
 if(temp == check)
  ;
 else
  ;
}
unsigned char recv_one_bit(void)
{
 unsigned char ch;
 while(!rx_flag);
 rx_flag = ;
 while(!rx_flag);
 rx_flag = ;
 while(!rx_flag);
 rx_flag = ;
 ch = read_rx_line();
 while(!rx_flag);
 rx_flag = ;
 while(!rx_flag);
 rx_flag = ;
 return ch;
}
unsigned char is_start_bit(void)
{
 unsigned ;
 if(!rx_flag)
  ;
 rx_flag =;
 start_bit = read_rx_line(); //第一次采样RX
 if(start_bit)
  ;
 while(!rx_flag);
 rx_flag = ;
 start_bit = read_rx_line(); //第二次采样RX
 if(start_bit)
  ;
 while(!rx_flag);
 rx_flag = ;
 start_bit = read_rx_line(); //第三次采样RX
 if(start_bit)
  ;
 while(!rx_flag);  //第4次
 rx_flag = ;
 while(!rx_flag); //第5次
 rx_flag = ;
 ;
}
unsigned char is_stop_bit(void)
{
 return recv_one_bit();
}
unsigned char recv_one_byte(unsigned char *ch)
{
 unsigned ,temp=;
 unsigned ;
 *ch =;
 if(!is_start_bit())
  ;
 ;i++)
 {
  temp |= (recv_one_bit() << i);
 }
 )
 {
 }
 )
 {
          parriy_bit = recv_one_bit();
    if(!even_check(temp,parriy_bit))
   ;
 }
 )
 {
         parriy_bit = recv_one_bit();
   if(!odd_check(temp,parriy_bit))
   ;
 }
 else //Default:None
 {
 }
 )
 {
    if(!is_stop_bit())
        ;
 }
 )
 {
  if(!is_stop_bit())
   ;
  if(!is_stop_bit())
   ;
 }
 else //Default : stop =1
 {
  if(!is_stop_bit())
        ;
 }
 *ch = temp;
 ;
}
unsigned char recv_n_byte(unsigned char *buf)
{
 unsigned ;
    unsigned ;
 unsigned ;
 unsigned ;
 )
 {
  if(recv_one_byte(&ch))
  {
     buf[i++] = ch;
     count = ;
  }
  else
  {
   count++;
   if(count > timeout)
    break;
  }
 }
 return i;
}
void send_one_byte(unsigned char ch)
{
   unsigned ,parriy_bit;
   tx_flag = ;
   start_tx_timer();
   write_tx_line();
   while(!tx_flag);
   tx_flag = ;
   write_tx_line();

   ;i++)
   {
  while(!tx_flag);
  << i))
   write_tx_line();
  else
   write_tx_line();
  tx_flag = ;
   }
 )
    {

 }
  )
 {
  parriy_bit = make_even_bit(ch);
  while(!tx_flag);
  write_tx_line(parriy_bit);
  tx_flag = ;
 }
  )
 {
  parriy_bit = make_odd_bit(ch);
  while(!tx_flag);
  write_tx_line(parriy_bit);
  tx_flag = ;
 }
 else
 {
  ;
 }
 )
 {
  while(!tx_flag);
  write_tx_line();
  tx_flag = ;
 }
 )
 {
  while(!tx_flag);
  write_tx_line();
  tx_flag = ;
  while(!tx_flag);
  write_tx_line();
  tx_flag = ;
 }
 else
 {
  while(!tx_flag);
  write_tx_line();
  tx_flag = ;
 }
 while(!tx_flag);
    tx_flag = ;
    stop_tx_timer();
}
void send_n_byte(unsigned char *buf,unsigned char num)
{
    unsigned ;
 ;i<num;i++)
 {
  send_one_byte(buf[i]);
 }
}
-----------------------------main.c------------------------------
void main(void)
{
 SetSysClk(PLL_CLK_128MHz);//
 )
    {
     send_n_byte("hello world !\r\n",sizeof("hello world !\r\n"));
     _delay_ms();
     )
     {
      ret = recv_n_byte(buf);
      if(ret)
      {
       send_n_byte(buf,ret);
      }
     }
    }
}
上一篇:LeetCode--204--计数质数


下一篇:arcgis for js开发之路径分析