i want to send Temperature sensor and Co2 sensor reading form Tm4C123 vis Bluetooth to app on the phone. I have some code here, i can display CO2 and Temperature reading on the LCD screen, but i can not send these through UART. Hoping anyone can give me some points. Thanks
#include <stdio.h> #include <stdint.h> #include "tm4c123gh6pm.h" #include "ST7735.h" #include "PLL.h" #include "SysTick.h" #include <stdbool.h> #include "CO2_Sensor.h" #include "Temp_Header.h" #include "Bluetooth.h" void EnableInterrupts(void); void WaitForInterrupt(void); extern uint8_t rx_data[8]; extern uint8_t rx_index; double co2_calculation; int main(void){ PLL_Init(); //Initilize PLL to 80MHz SysTick_Init(); //Initilize SysTick Timer ST7735_InitR(INITR_REDTAB); //Initilize LCD Screen EnableInterrupts(); CO2_UART_Init (1, 0,' ', 9600); // initialize co2 uart tempSensor_Init(); //Bluetooth_UART_Init(1, 0,' ', 9600); while(1){ sendBytes(); WaitForInterrupt(); co2_calculation = rx_data[3]*256+rx_data[4]; ST7735_SetCursor(10,6); ST7735_DrawString(11, 6, " ", 0xFFFF); ST7735_OutUDec(co2_calculation); ST7735_DrawString(11,8,"ppm ", 0xF69F); ST7735_SetCursor(11,11); // Set location of temp read number in lCD ST7735_OutUDec(GetTemp()); // Display results // int8_t temp = GetTemp(); // int16_t co2_concentration = co2_calculation; // // Split the 16-bit integer into two 8-bit integers // int8_t co2_high_byte = (co2_concentration >> 8) & 0xFF; // int8_t co2_low_byte = co2_concentration & 0xFF; // // Send the data over Bluetooth // UART_Tx(temp); // Send the temperature as an 8-bit integer // UART_Tx(co2_high_byte); // Send the high byte of the CO2 concentration as an 8-bit integer // UART_Tx(co2_low_byte); // Send the low byte of the CO2 concentration as an 8-bit integer // SysTick_Wait10ms(15); } }
#include <stdio.h> #include "tm4c123gh6pm.h" #include <stdint.h> #include "SysTick.h" double Bluetooth_baudCalc = 0; double Bluetooth_fractionBaud = 0; int Bluetooth_integerBaud = 0; int Bluetooth_calcFractionBaud = 0; int Bluetooth_regBits = 0; int Bluetooth_Parity = 0; void Bluetooth_UART_Init(int Bluetooth_bits,int Bluetooth_stopBits, char Bluetooth_parity, double Bluetooth_baudRate){ volatile uint32_t delay; SYSCTL_RCGCUART_R = 0x80; // UART module 7 enable and activate clock SYSCTL_RCGCGPIO_R |= 0x10; // activate clock for Port E UART7_CTL_R = 0; // Disable UART7 GPIO_PORTE_AFSEL_R = 0x03; // set alt funct on PE0 and 1 GPIO_PORTE_PDR_R = 0x10; GPIO_PORTE_PCTL_R = 0x00000011; GPIO_PORTE_DIR_R = 0x02; // PF0 in, PF1 output GPIO_PORTE_DEN_R = 0x03; // enable digital GPIO_PORTE_AMSEL_R = 0x00; // disable analog on PE // initialize s function of stopbits Bluetooth_stopBits = Bluetooth_stopBits <<3; //Shift 4 places to be in the bit 3 position. //initialize uart interrupt UART7_IM_R = 0x10; // Interrupt is sent to controller on receive NVIC_EN1_R = 0x80000000; // Enable interrupt on port E Interrupt 63 UART 7 NVIC_PRI15_R = 0xE0000000; // Set priority setting PRI7 // Calculate Baud Rate Bluetooth_baudCalc = 80000000/(16*Bluetooth_baudRate); Bluetooth_integerBaud = Bluetooth_baudCalc; Bluetooth_fractionBaud = Bluetooth_baudCalc - Bluetooth_integerBaud; Bluetooth_calcFractionBaud =(Bluetooth_fractionBaud * 64 + 0.5); // Assign # of bits registrar values to switch case. switch(Bluetooth_bits) { case 1: { Bluetooth_regBits = 0x60; //8 bits break; } case 2: { Bluetooth_regBits = 0x40; // 7bits break; } case 3: { Bluetooth_regBits = 0x20; // 6 bits break; } default: { Bluetooth_regBits = 0x00; //5 bits break; } } //Setting registar value for parity switch(Bluetooth_parity) { case '0': //Parity even { Bluetooth_Parity = 0x86; break; } case '1': //Parity odd { Bluetooth_Parity = 0x82; break; } default: //Parity off { Bluetooth_Parity = 0x00; break; } } // initialize uart UART7_CTL_R = 0; // Disable UART7 UART7_IBRD_R = Bluetooth_integerBaud; //Assigning integer part UART7_FBRD_R = Bluetooth_calcFractionBaud; //Assigning fractional part UART7_LCRH_R |= Bluetooth_regBits; //Oring the word length for the line control UART7_LCRH_R |= Bluetooth_stopBits; //Oring 2 or 1 stop bits UART7_LCRH_R |= Bluetooth_Parity; //Assigning parity to registar UART7_IFLS_R = 0x00; // enable FIFO UART7_LCRH_R |= 0x10; // set fifo level UART7_CC_R = 0x00; //Want to use system clock UART7_CTL_R |= 0x0301; //Turning uart1 back on, transmit and recevie back on UART7_CC_R = 0x00; // UART runs on system clock UART7_CTL_R = 0x0301; // Enable UART, send, receive } void UART_Tx(int8_t Data){ UART7_DR_R = Data;// Send the data through UART transmit pin }
#include <stdio.h> #include <stdint.h> #include "stdint.h" #include "PLL.h" #include "Systick.h" #include "tm4c123gh6pm.h" int firstByte; // Variable for first byte to be assigned. int secByte; // Varible for second byte received. int temp; // Variable for temp sensor data void tempSensor_Init(void) { // Function initializes port E pin 4 and 5 I2C to transmit and recieve from sensor. SYSCTL_RCGCGPIO_R |= 0x10; // Turns on port E clock. SYSCTL_RCGCI2C_R |= 0x04; // turns on PWM system GPIO_PORTE_AFSEL_R = 0x30; // Select alternate function GPIO_PORTE_PCTL_R = 0x00330000; // Selects M0PWM6 for PE0 GPIO_PORTE_ODR_R = 0x20; // Sets pin 4 and 5 to open drain. GPIO_PORTE_DIR_R = 0x20; // Sets PE0 as an output. GPIO_PORTE_AMSEL_R = 0x00; // Disable analog mode. GPIO_PORTE_DEN_R = 0x30; // Enable digital mode. I2C2_MCR_R = 0x10; // Enable master. I2C2_MTPR_R = 0x27; // Sets desired clock speed. do{ // do{ I2C2_MSA_R = 0x90; // Master transmit slave address I2C2_MDR_R = 0x01; // Master transmits pointer and memory function I2C2_MCS_R = 0x03; // Start and run I2C2_MDR_R = 0x60; // Check for busy I2C2_MCS_R = 0x01; // Data run I2C2_MDR_R = 0x00; // configure second byte of sensor. I2C2_MCS_R = 0x05; // Data run/stop while (I2C2_MCS_R & (1<<6)); // Wait if busy } while (I2C2_MCS_R & (1<<1)); // Check for error and repeat if error found. } int GetTemp(void){ // Function enables controller to communicate with sensor and set result to variable. do{ I2C2_MSA_R = 0x90; // Master transmit slave address and 0 bit set 0 write. I2C2_MDR_R = 0x00; // Pointer/memory location I2C2_MCS_R = 0x03; // Data start and run. while (I2C2_MCS_R & (1 << 6)); // Check busy and wait if busy. } while (I2C2_MCS_R & (1 << 1)); // Check error status, repeat if error. I2C2_MSA_R = 0x91; // Receive from slave and 0 bit set to 1. I2C2_MCS_R = 0x0B; // Data start, run, acknowledge while (I2C2_MCS_R & (1 << 6)); firstByte = I2C2_MDR_R; // Assign data to FirstByte variable I2C2_MCS_R = 0x09; // Run data and ack while (I2C2_MCS_R & (1 << 6)); // Check bus busy, if busy wait. secByte = I2C2_MDR_R; // Assigns data for secByte variable firstByte = (firstByte << 4); // Shifts firstByte 4 to the left. secByte = (secByte >> 4); // Shifts secByte 4 to the right. temp = (firstByte + secByte); // Adds data from both bytes and assigns to temp variable. I2C2_MCS_R = 0x05; // Run data, stop. if (temp & (1 << 12)) { // If signed bit is 1, if 1 convert from 2s complement. temp = ~temp + 1; // Convert to negative temp temp = temp * 0.0625; temp = temp * -1; return temp; } // Convert to positive temp. else { temp = temp * 0.0625; return temp; } }
#include <stdio.h> #include <stdint.h> #include "tm4c123gh6pm.h" #include "ST7735.h" #include "PLL.h" #include "SysTick.h" #include <stdbool.h> volatile uint8_t rx_data[8]; volatile uint8_t rx_index = 0; double baudCalc = 0; double fractionBaud = 0; int integerBaud = 0; int calcFractionBaud = 0; int regBits = 0; int Parity = 0; void CO2_UART_Init(int bits,int stopBits, char parity, double baudRate){ volatile uint32_t delay; SYSCTL_RCGCUART_R = 0x02; // UART module 1 enable and activate clock SYSCTL_RCGCGPIO_R |= 0x00000002; // 1) activate clock for Port B UART1_CTL_R = 0; // Disable UART1 GPIO_PORTB_AFSEL_R = 0x03; // set alt funct on PB0 and 1 GPIO_PORTB_PDR_R = 0x10; GPIO_PORTB_PCTL_R = 0x00000011; GPIO_PORTB_DIR_R = 0x02; // PB0 in, PB1 output GPIO_PORTB_DEN_R = 0x03; // enable digital GPIO_PORTB_AMSEL_R = 0x00; // disable analog on PB // initialize s function of stopbits stopBits = stopBits <<3; //Shift 4 places to be in the bit 3 position. //initialize uart interrupt UART1_IM_R = 0x10; // Interrupt is sent to controller on receive NVIC_EN0_R = 0x40; // Enable interrupt UART 1 NVIC_PRI15_R = 0xE0000000; // Set priority setting PRI7 // Calculate Baud Rate baudCalc = 80000000/(16*baudRate); integerBaud = baudCalc; fractionBaud = baudCalc - integerBaud; calcFractionBaud =(fractionBaud * 64 + 0.5); // Assign # of bits registrar values to switch case. switch(bits) { case 1: { regBits = 0x60; break; } case 2: { regBits = 0x40; break; } case 3: { regBits = 0x20; break; } default: { regBits = 0x00; break; } } //Setting registar value for parity switch(parity) { case '0': //Parity even { Parity = 0x86; break; } case '1': //Parity odd { Parity = 0x82; break; } default: //Parity off { Parity = 0x00; break; } } // initialize uart UART1_CTL_R = 0; // Disable UART1 UART1_IBRD_R = integerBaud; //Assigning integer part UART1_FBRD_R = calcFractionBaud; //Assigning fractional part UART1_LCRH_R |= regBits; //Oring the word length for the line control UART1_LCRH_R |= stopBits; //Oring 2 or 1 stop bits UART1_LCRH_R |= Parity; //Assigning parity to registar UART1_IFLS_R = 0x40; // set fifo level UART1_LCRH_R |= 0x10; // enable fifo UART1_CC_R = 0x00; //Want to use system clock UART1_CTL_R |= 0x0301; //Turning uart1 back on, transmit and recevie back on UART1_CC_R = 0x00; // UART runs on system clock UART1_CTL_R = 0x0301; // Enable UART, send, receive } void sendBytes(void) { // Write four bytes to UART1 TX FIFO UART1_DR_R = 0x10; UART1_DR_R = 0x01; UART1_DR_R = 0x03; UART1_DR_R = 0xEC; } void UART1_Handler(void) { // Read received data from UART1 RX FIFO while ((UART1_FR_R & 0x10) == 0) { rx_data[rx_index++] = UART1_DR_R; UART1_ICR_R = 0x10; // clear interrupt } return; }
For this kind of problems, it is better for you to connect the debugger with JTAG probe to check what is the root cause. For Cortex-M processors, you can use J-Link proble with Keil or similar debuggers.
Most likely, it is just your driver code issue.