Tuesday, October 11, 2016

Serial Communication Using UART of Cortex-M3

Click on the following links to read the previous posts on Cortex-M3
Blink LED Using Cortex-M3
LED Blink Using Timer of Cortex-M3
Interfacing RTC PCF8523 with Cortex-M3
Using Analog To Digital Converter of Cortex-M3
PS/2 Keypad Interfacing with LPC1343 Micro-Controller


Hey guys, in this post, we will send the RTC Data, Potentiometer ADC Raw Value and Voltage value in milli-volts over Serial Port using LPC1343.For sensing the data and reading the RTC readings you can refer the links posted at the top of this page.
Schematic Diagram

First of all we will send the data in Semi-Hosting windows of CoCoox IDE and then we will send data over Serial Port.

The code is as follow:

#include "config.h"  
 #include "lpc13xx_gpio.h"  
 #include "adc.h"  
 #include "uart.h"  
 #include "pcf8523.h"  
 #ifdef SEMIHOSTING  
 #include "semihosting.h"  
 #endif  
 int main()  
 {  
  u8_t second, minute, hour;  
  u8_t date, month, year;  
  u32_t time = 0;  
  u16_t adc_value = 0;  
  u16_t voltage  = 0;  
  u8_t receive = 0;  
  // Initialize System  
  InitializeSystem();  
  // Initialize RTC  
  PCF8523_Init();  
  // Initialize ADC  
  ADC_Init();  
  // Initialize UART  
  UART_Init();  
  // Set Time and Date  
  PCF8523_SetTimeDate(0x20, 0x10, 0x00, 0x05, 0x04, 0x15);  
 #ifndef SEMIHOSTING  
  do  
  {  
   UART_Write_Text((unsigned char*)"\nPress \"s\" to Start communication?\n");  
   receive = UART_Read();  
  } while (receive != 's');  
 #endif  
  while(1)  
  {  
 #ifndef SEMIHOSTING  
   if(millis() - time > 2000 )  
 #endif  
   {  
    time = millis();  
    // Read Seconds, Minutes, Hours, Date, Month and Year from RTC  
    second = PCF8523_Read(SECONDS);  
    minute = PCF8523_Read(MINUTES);  
    hour  = PCF8523_Read(HOURS);  
    date  = PCF8523_Read(DAYS);  
    month = PCF8523_Read(MONTHS);  
    year  = PCF8523_Read(YEARS);  
    // Read ADC Count from Channel-0  
    adc_value = ADC_Read(ADC_CHANNEL_0);  
    voltage = adc_value*3300 / 1023;  
 #ifndef SEMIHOSTING  
    // Display Time, Date, ADC Raw Count and Voltage on UART  
    // Time  
    UART_Write_Text((unsigned char*)"Time:");  
    UART_Write(BCD2UpperCh(hour));  
    UART_Write(BCD2LowerCh(hour));  
    UART_Write(':');  
    UART_Write(BCD2UpperCh(minute));  
    UART_Write(BCD2LowerCh(minute));  
    UART_Write(':');  
    UART_Write(BCD2UpperCh(second));  
    UART_Write(BCD2LowerCh(second));  
    UART_Write(' ');  
    // Date  
    UART_Write_Text((unsigned char*)"Date:");  
    UART_Write(BCD2UpperCh(date));  
    UART_Write(BCD2LowerCh(date));  
    UART_Write(':');  
    UART_Write(BCD2UpperCh(month));  
    UART_Write(BCD2LowerCh(month));  
    UART_Write(':');  
    UART_Write(BCD2UpperCh(year));  
    UART_Write(BCD2LowerCh(year));  
    UART_Write(' ');  
    // Raw ADC Count, Maximum Value is 1023 and Minimum 0  
    UART_Write_Text((unsigned char*)"ADC:");  
    UART_Write(adc_value/1000 | 0x30);  
    UART_Write((adc_value/100)%10 | 0x30);  
    UART_Write((adc_value/10)%10 | 0x30);  
    UART_Write(adc_value%10 | 0x30);  
    UART_Write(' ');  
    // Maximum Voltage is 3300mV, and Minimum is 0mV  
    UART_Write_Text((unsigned char*)"Voltage:");  
    UART_Write(voltage/1000 | 0x30);  
    UART_Write((voltage/100)%10 | 0x30);  
    UART_Write((voltage/10)%10 | 0x30);  
    UART_Write(voltage%10 | 0x30);  
    UART_Write_Text((unsigned char*)"mV\n");  
 #endif  
   }  
 #ifdef SEMIHOSTING  
   printf("Time:%x:%x:%x Date:%x/%x/%x , ADC:%d, Voltage:%dmV \n", \  
      hour, minute, second, date, month, year, adc_value, voltage);  
 #endif  
  }  
 }  
 void UART_IRQHandler(void)  
 {  
  // Process UART Interrupt, if Enabled  
 }


The functions are created to send and receive data, with there source files. Click here to download the complete code. The code be ported easily for any other IDE like KeiluVision or IAR (In most of the cases no changes are required).


Complete Working of Code is present in the following video.

No comments:

Post a Comment