code nhận dữ liệu UART với ngắt

ga_cong

Kỹ sư
#1
Thấy có người hỏi nên tôi đưa code cho bạn nào cần. Để sử dụng cần add 2 thư viện là stm8s_uart1.c và stm8s_clk.c
Mã:
/**
  ******************************************************************************
  * @file    Project/main.c
  * @author  MCD Application Team
  * @version V2.2.0
  * @date    30-September-2014
  * @brief   Main program body
   ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */


/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"

/* Private defines -----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
{
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
   char ch;
   ch=UART1_ReceiveData8();
   if(ch=='a')
     GPIO_WriteHigh(GPIOD,GPIO_PIN_3);
   else
   if (ch=='b')
     GPIO_WriteLow(GPIOD,GPIO_PIN_3);
}
void main(void)
{
  GPIO_Init(GPIOD,GPIO_PIN_3,GPIO_MODE_OUT_PP_LOW_FAST);
  UART1_Init((uint32_t)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
  UART1_ITConfig(UART1_IT_RXNE_OR , ENABLE);
  UART1_Cmd(ENABLE);
   enableInterrupts();
  /* Infinite loop */
  while (1)
  {
  }

}

#ifdef USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval : None
  */
void assert_failed(u8* file, u32 line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
 

hieuktdt12

Học sinh phổ thông
#2
bác cho em hỏi em bị lỗi này phải xử lý sao ạ
Error[Pe020]: identifier "uint32_t" is undefined E:\danglam\STM\lap trinh\UART\main.c 51
 

hieuktdt12

Học sinh phổ thông
#3
em đang mắc ở phần thư viện các bác ạ, em biên dịch thì ko lỗi nhưng ko hiểu sao lại lỗi khi nạp chương trình
Error[Pe136]: struct "CLK_struct" has no field "CANCCR" E:\danglam\STM\lap trinh\UART\STM8S_StdPeriph_Driver\src\stm8s_clk.c 79
Error[Pe020]: identifier "CLK_CANCCR_RESET_VALUE" is undefined E:\danglam\STM\lap trinh\UART\STM8S_StdPeriph_Driver\src\stm8s_clk.c 79
Error[Pe136]: struct "CLK_struct" has no field "CANCCR" E:\danglam\STM\lap trinh\UART\STM8S_StdPeriph_Driver\src\stm8s_clk.c 562
Error[Pe020]: identifier "CLK_CANCCR_CANDIV" is undefined E:\danglam\STM\lap trinh\UART\STM8S_StdPeriph_Driver\src\stm8s_clk.c 562
Error[Pe136]: struct "CLK_struct" has no field "CANCCR" E:\danglam\STM\lap trinh\UART\STM8S_StdPeriph_Driver\src\stm8s_clk.c 565
 

ga_cong

Kỹ sư
#4
add 2 file stm8s_clk.c và stm8s_uart1.c vào. trong file stm8s.h thì chọn là stm8s003
 

hieuktdt12

Học sinh phổ thông
#5
bác cho em xin cả cái project đc ko ạ, em làm mãi mà nó cứ báo lỗi hoài thôi
 

hieuktdt12

Học sinh phổ thông
#8
thank bác nhiều, nhưng em gặp lỗi truyền ký tự lên máy tính bị sai và không sử đụng được lệnh if(ch=='a') là sao vậy ạ, bác có cách khắc phục không ạ
khi em nhận ký tự từ máy xuống thì em nhận được mã acii của ký tự em kiểm tra thì đúng nhưng ko so sánh để thực hiện lệnh khác được
 

hieuktdt12

Học sinh phổ thông
#10
code của em đây bác xem giúp em với
Mã:
/**
  ******************************************************************************
  * @file    Project/main.c
  * @author  MCD Application Team
  * @version V2.2.0
  * @date    30-September-2014
  * @brief   Main program body
   ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */


/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
#include <stdio.h>
#include <string.h>
#include <iostm8s003f3.h>
#define LED  PD_ODR_ODR4
#define LCD_RS  PB_ODR_ODR4
#define LCD_EN  PC_ODR_ODR3
#define LCD_D4    PC_ODR_ODR4
#define LCD_D5    PC_ODR_ODR5
#define LCD_D6    PC_ODR_ODR6
#define LCD_D7    PC_ODR_ODR7
#define Line_1 0x80
#define Line_2 0xC0      /////// lcd
#define Clear_LCD 0x01
char  M[32];
void delay(unsigned long int t)
{
  unsigned long int i=0;
  for(i=0;i<t;i++);
}

/* Private defines -----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
{
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
   char ch;
//  int i;
   ch=UART1_ReceiveData8();
  
  if(ch==53)
  {
     GPIO_WriteLow(GPIOD,GPIO_PIN_4);
     delay(10000);
     GPIO_WriteHigh(GPIOD, GPIO_PIN_4);
   delay(10000);
  }
}
/*
void delay_us(unsigned int time)
{
   unsigned int i;
   for(i=0;i<time;i++)
   {
      ;// khong lam gi ca
   }
}
void delay_ms(unsigned time)
{
    unsigned int i,j;
    for(i=0;i<time;i++)
    for(j=0;j<125;j++)
    {
      ;// khong lam gi ca
    }
}
void LCD_Enable(void)
{
  LCD_EN = 1;
delay_us(3);
  LCD_EN=0;
  delay_us(50);
}
void LCD_Send4Bit( unsigned char Data )
{
LCD_D4= Data & 0x01;
LCD_D5= (Data>>1)&1;
LCD_D6= (Data>>2)&1; // gui data 4 bit
LCD_D7= (Data>>3)&1;
}
void LCD_SendCommand (unsigned char command )
{
LCD_Send4Bit  ( command >>4 );
LCD_Enable () ;                                      // gui data 8 bit
LCD_Send4Bit  ( command  );
LCD_Enable () ;
}
void LCD_Init ( void )
{
LCD_Send4Bit(0x00);
delay_ms(20);
LCD_RS=0;
LCD_Send4Bit(0x03);
LCD_Enable();
delay_ms(5);                         // khai bao LCD che do 4bit 5x7
LCD_Enable();
delay_us(100);
LCD_Enable();
LCD_Send4Bit(0x02);
LCD_Enable();
LCD_SendCommand( 0x28 );      // giao thuc 4 bit, hien thi 2 hang, ki tu 5x8
LCD_SendCommand( 0x0c);     // cho phep hien thi man hinh
LCD_SendCommand( 0x06 );      // tang ID, khong dich khung hinh
LCD_SendCommand( 0X01 ); // xoa toan bo khung hinh
}
//////////////////////
void LCD_Gotoxy(unsigned char x, unsigned char y)// x:vi tri con tro( 0-->15), y : vi tri dong ( 0: dong 1,1 dong 2)
{
  unsigned char address;
  if(!y)
  address = (Line_1+x);
  else  address = (Line_2+x);     // xac dinh vi tri con tro
  delay_us(300);
  LCD_SendCommand(address);
  delay_us(50);
}
void LCD_Clear()
{
  LCD_SendCommand( Clear_LCD );  // xoa LCD
  delay_us(10);
}
void LCD_PutChar ( unsigned char Data )  // LCD_Putchar('x')
{
LCD_RS=1;
LCD_SendCommand( Data );  //  xuat data 8 bit 1 ki tu
LCD_RS=0 ;
}
void LCD_Puts (char *s)// LCD_Puts("xxx")
{
   while (*s)
   {
      LCD_PutChar(*s);  // xuat chuoi data "xxx"
      s++;
   }
}
void LCD_Putn(unsigned char c)
{
  LCD_EN = 1;// Cho fep muc cao
  LCD_RS=1;// Ghi du lieu                // xuat so xxx+48
  LCD_PutChar(c);// Gia tri C
  LCD_EN = 0;// Xung cao xuong thap
  delay_ms(10);// Cho
}

*/

//////////////////////////////////////////
void InitClock(void)
{
 
    CLK_DeInit();
    CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);      //f_Master = HSI/1 = 16MHz
    CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV16);            //f_CPU = f_Master/1 = 16MHz
    while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY)!=SET);     //wait until HSI ready
}
void InitGPIO(void)
{
  PB_DDR = 0xFF;
  PB_CR1 = 0xFF;
  PC_DDR = 0xFF;
  PC_CR1 = 0xFF;
  PD_DDR = 0xFF;
  PD_CR1 = 0xFF;
}
/*
void uart1_putchar(char c) {
while(!(UART1->SR & UART1_SR_TXE));
UART1->DR = c;
}
*/
void main(void)
{
//  uint8_t c;
  //InitGPIO();
  InitClock();
  GPIO_Init(GPIOD,GPIO_PIN_4,GPIO_MODE_OUT_PP_LOW_FAST);
  UART1_DeInit();
  UART1_Init((uint32_t)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
  UART1_ITConfig(UART1_IT_RXNE_OR , ENABLE);
  UART1_Cmd(ENABLE);
  // enableInterrupts();
   GPIO_WriteHigh(GPIOD,GPIO_PIN_4);
//   uart1_putchar(48);
   // LCD_Init();
  /// LCD_Clear();LED=0;
  /* Infinite loop */
  while (1)
  {
            while (UART1_GetFlagStatus(UART1_FLAG_RXNE) == RESET);
          //      c = UART1_ReceiveData8();
        UART1_SendData8("abc");
        // Loop until the end of transmission
        while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET);
      //LCD_Gotoxy(0,0);
  //   LCD_Puts("STM8 DEMO LCD   ");
    
  }
 
}

#ifdef USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval : None
  */
void assert_failed(u8* file, u32 line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
 

Quảng cáo Google