打印

stm8串口中断接收

[复制链接]
2996|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
vectory|  楼主 | 2011-7-21 13:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 vectory 于 2011-7-21 13:36 编辑

/* MAIN.C file
*
* Copyright (c) 2002-2005 STMicroelectronics
*/

#include <stm8s105s6.h>
#include <stm8s105c_s.h>
void uart_Interrupt (void);
extern unsigned char data;
static void delay5ms(void)
{
unsigned char i;
for(i=0;i<100;i++);
}
static void delay100ms(void)
{
unsigned char i;
for(i=0;i<20;i++)
  {for(i=0;i<100;i++);}
  }

void Sendchar(unsigned char byte)
{   PC_ODR =0X02;
  delay5ms();
   UART2_SR;
  UART2_DR=byte; //
    //while(!(UART2_SR & 0X80));
    //while(!(UART2_SR & 0X40));
   while (!_btst(UART2_SR,7));
   while (!_btst(UART2_SR,6));
  UART2_DR;
   //delay5ms();
  PC_ODR = 0X00;
  //delay5ms();
  
}
unsigned char k_read (void)
{if((UART2_SR & 0X20))
  {
   if((UART2_SR & 0X28))
   {
   return UART2_DR;
   }
   }
}

main()
{
//unsigned char a[20];
  
unsigned char i;
unsigned char a=0,flag;
void Sendchar(unsigned char byte);
void senddata(unsigned char buff[],unsigned int length);
void startsend(unsigned char buff[],unsigned char length);
unsigned char k_read (void);
//char putchar (char c);
  
CLK_CKDIVR&= 0X00;
CLK_CKDIVR|= 0X08;  //clk= 8MHZ
UART2_CR2 = 0X00;//disable rx tx
UART2_CR1 = 0X00;//uart enable m=0 8 bits data
UART2_CR3 = 0X00;//stop bit = 1
//UART2_BRR2 = 0X0B;//baud rate = 115200
//UART2_BRR1 = 0X08;
UART2_BRR2 = 0X01;//baud rate = 9600
UART2_BRR1 = 0X34;
//UART2_BRR2 = 0X0B;//baud rate = 14400
//UART2_BRR1 = 0X22;
UART2_CR2  = 0x1c;
//UART2_SR;

  _bset(PC_DDR,1);
_bset(PC_CR1,1);
_bset(PC_CR2,1);

_bset (PC_ODR,1);  
_asm("rim");
  while (1)
{

   UART2_CR2|=0x20;//接受中断开启
   PC_ODR= 0X00;
  delay100ms();
   
   PC_ODR = 0X02;
   UART2_DR = a;
   //   while (!_btst(UART2_SR,7));
    // while (!_btst(UART2_SR,6));
   Sendchar(a);
  
  a++;
    delay5ms();
}
}

@near @interrupt void uart_Interrupt (void)
{
// if(_btst(UART2_SR , 5))//UART2_SR & 0X20 ==0X20
  {
   data = UART2_DR;
   _bset(UART2_SR , 5);//UART2_SR &= 0XDF;
   //UART2_DR =data;
  }
   //if (_btst (UART2_SR ,3))
  // {
// UART2_DR;
// }

  PC_ODR = 0X02;

//UART2_CR2 &=(~0x20);//接受中断关闭

是利用485通信PC1接485使能端,单步可以实现接收数据,RUN的话一直是00;
各位大哥帮忙看看

}
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

22

帖子

1

粉丝