Working on a RX65N microcontroller and ultrasonic sensor on e2studio. i managed to like the lcd works and "show distance is" but i can't figure it the every so change distance value and all i see out there are only arduino codes... so far my main code is:
void main(void)
{
// [DO NOT REMOVE] array declaration
char img_buf[IMG_BUF_SZ] = {0};
unsigned char i;
unsigned short distance, pulsewidth;
// P05, P07 initialization for TFT_RST and TCH_RST of LCD respectively
// output logic high from both P05 and P07
// Initialize P05 for TFT_RST
PMR0 = PMR0 & 0xDF; // Configure P05 as GPIO (clear corresponding bit in PMR0)
PDR0 = PDR0 | 0x20; // Set P05 as output (set corresponding bit in PDR0, 00100000)
ODR0 = ODR0 & 0xDF; // Set P05 as open CMOS output (clear corresponding bit in ODR0)
PODR0 = PODR0 | 0x20; // Set P05 to output logic high (set corresponding bit in PODR0)
// Initialize P07 for TCH_RST
PMR0 = PMR0 & 0x7F; // Configure P07 as GPIO (clear corresponding bit in PMR0)
PDR0 = PDR0 | 0x80; // Set P07 as output (set corresponding bit in PDR0)
ODR0 = ODR0 & 0x7F; // Set P07 as open CMOS output (clear corresponding bit in ODR0)
PODR0 = PODR0 | 0x80; // Set P07 to output logic high (set corresponding bit in PODR0)
// P14, P17 initialization for TFT_WR and LED_BL of LCD respectively
// output logic high from both P14 and P17
// Initialize P14 for TFT_WR
PMR1 = PMR1 & 0xEF; // Configure P14 as GPIO (clear corresponding bit in PMR1)
PDR1 = PDR1 | 0x10; // Set P14 as output (set corresponding bit in PDR1)
ODR1 = ODR1 & 0x7F; // Set P14 as open CMOS output (clear corresponding bit in ODR1)
PODR1 = PODR1 | 0x10; // Set P14 to output logic high (set corresponding bit in PODR1)
// Initialize P17 for LED_BL
PMR1 = PMR1 & 0x7F; // Configure P17 as GPIO (clear corresponding bit in PMR1)
PDR1 = PDR1 | 0x80; // Set P17 as output (set corresponding bit in PDR1)
ODR1 = ODR1 & 0x7F; // Set P17 as open CMOS output (clear corresponding bit in ODR1)
PODR1 = PODR1 | 0x80; // Set P17 to output logic high (set corresponding bit in PODR1)
// Initialize P22 pin on microcontroller. This pin connects to TRIG pin of ultrasonic sensor
PMR2 = PMR2 & 0xFB; // Clear bit 2 (0xFB = 11111011) to set P22 to GPIO mode
PDR2 = PDR2 | 0x04; // Set bit 2 (0x04 = 00000100) to configure P22 as an output
ODR02 = ODR02 & 0xEF;
PODR2 = PODR2 & 0xFB; // Clear bit 2 (0xFB = 11111011) to set logic low
// Initialize PB5 pin on microcontroller. This pin connects to ECHO pin of ultrasonic sensor
// Set all pins of Port B as input, with pull-up resistor enabled
PMRB = 0x00; // Clear all bits in PMRB to configure Port B as GPIO
PDRB = 0x00; // Clear all bits in PDRB to configure Port B as input
PCRB = 0xFF; // Set all bits in PCRB to enable pull-up resistors
// Start SPI channel 0 for LCD
R_Config_RSPI0_Start();
// initialize LCD and fill entire screen with GREEN color
_LCD_tftreset();
_LCD_tpreset();
_LCD_tftInit();
spi_flg = 0;
_2D_draw_Clear(GREEN);
_2D_draw_Text("Dist is: ", 10, 10, BLACK);
// Initialize TMR2 (timer configuration)
R_Config_TMR2_Create();
// Start the TMR2 timer
R_Config_TMR2_Start();
while(1)
{
// perform ultrasonic distance measurement
// send a 10us trigger pulse
PODR2 = PODR2 | 0x04; //send P22 to high
MCU_Delay(10);
PODR2 = PODR2 & 0xFB; //send P22 to low
MCU_Delay(1000);
// wait for ECHO to be logic high
while((PIDRB & 0x20) == 0); // Wait for PB5 (ECHO) to go high
pulsewidth = 0;
// wait until ECHO becomes logic low
while ((PIDRB & 0x20) != 0)
{
pulsewidth++; // time lapse, each increment is 10us
MCU_Delay(10); // Adjusted delay for more accurate timing
}
//distance = 0.01715 * pulsewidth; // calculate distance in cm, distance = 0.01715 * time, where time is in us
distance = (pulsewidth * 34300) / (2 * 1000000);
// display distance on LCD
for (i=0;i<IMG_BUF_SZ;i++) img_buf[i] = 0;
sprintf(img_buf, "%d cm", distance);
// display ascii char on LCD
_2D_draw_Rect(10, 30, 88, 26, WHITE);
_2D_draw_Text((uint8_t *)img_buf, 14, 34, BLACK);
// Delay 1 second before updating
MCU_Delay(1000); // Delay for 1 second (1,000,000 microseconds)
}
R_Config_TMR2_Stop();
}
// function definition
void LCDInit(void)
{
// start SPI for LCD
R_Config_RSPI0_Start();
// TFT reset
_LCD_tftreset();
// Touch Panel Reset
_LCD_tpreset();
// TFT Initialization
_LCD_tftInit();
// clear SPI flag variable
spi_flg = 0;
// clear LCD screen to GREEN color
_2D_draw_Clear(GREEN);
}
void _UserRSPI_send_addr(unsigned long var1)
{
// declare unsigned long variable and assign var1 to variable
// this variable stores data to be sent via RSPI0
unsigned long abc = var1;
// set LCD controller to CMD mode
_RSPI_Write(CMD_WRITE);
// send data via RSPI0
R_Config_RSPI0_Send(&abc, 1);
// wait for SPI transmission to finish
while (spi_flg == 0);
// clear SPI flag variable
spi_flg = 0;
// set LCD controller to Data mode
_RSPI_Write(DAT_WRITE);
}
void _UserRSPI_send_dat(unsigned long var1)
{
// declare unsigned long variable and assign var1 to variable
// this variable stores data to be sent via RSPI0
unsigned long abc = var1;
// send data via RSPI0
R_Config_RSPI0_Send(&abc, 1);
// wait for SPI transmission to finish
while (spi_flg == 0);
// clear SPI flag variable
spi_flg = 0;
}
void MCU_Delay(unsigned long us) // delay in microseconds (approximate) system clock 120MHz
{
us *= 8;
us--;
while (us)
{
nop(); nop(); nop(); nop(); nop();
nop(); nop(); nop(); nop(); nop();
us--;
}
}
Apparently i need to use a TMR Config to constantly updates the value.