cancel
Showing results for 
Search instead for 
Did you mean: 

STM8: Problem displaying ADC value

ankhola
Associate III

Hi Guys,

Based on some tutorials, I am trying to execute ADC module of STM8S105 and display the ADC result in 16x2 LCD display. The problem is : thousandth digit place is showing alphabetic character right from "A"  when I increase voltage on ADC channel from 0. Please see my code as well as adc.h file. I am eagerly awaiting someone to suggest where I have made the mistake.

 

 #define LCD_RS     GPIOD, GPIO_PIN_2
 #define LCD_EN     GPIOD, GPIO_PIN_3
 #define LCD_DB4    GPIOD, GPIO_PIN_4
 #define LCD_DB5    GPIOD, GPIO_PIN_5
 #define LCD_DB6    GPIOD, GPIO_PIN_6
 #define LCD_DB7    GPIOD, GPIO_PIN_7
 
 #include "STM8S.h"
 #include "lcd.h"
#include "adc1.h"
main()
{
//Variable declarations

unsigned int test_var ;
char d4,d3,d2,d1;
gpio_adc_init();
adc_init();
Lcd_Begin();
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Print_String("STM8S103F3P3 LCD");
delay_ms(5000);
Lcd_Clear();
Lcd_Set_Cursor(1,1);
Lcd_Print_String("Circuit Digest");
Lcd_Set_Cursor(2,1);
Lcd_Print_String("Test: ");
while (1)


{ 
test_var = read_adc_value();
d4 = test_var%10 + '0';
d3 = (test_var/10)%10 + '0';
d2 = (test_var/100)%10 + '0';
d1 = (test_var/1000) + '0';
Lcd_Set_Cursor(2,6);
Lcd_Print_Char(d1);
Lcd_Print_Char(d2);
Lcd_Print_Char(d3);
Lcd_Print_Char(d4);
delay_ms(1000);
//test_var++;
}
}

#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
  while (1)
  {
  }
}
#endif

adc.h

 

uint16_t value;

void gpio_adc_init(void) {
	CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, ENABLE);
	GPIO_Init(GPIOB, GPIO_PIN_4, GPIO_MODE_IN_FL_NO_IT);
			}
										
void adc_init(void)   {												
ADC1 -> CSR = 0x04;
ADC1 -> CR1 =  0x40;
ADC1 -> CR2 = 0x00;
ADC1 -> TDRH = 0xFF;
ADC1 -> TDRL = 0xFF;
ADC1 -> CR1 |= ADC1_CR1_ADON;
			 }
						  
										
uint16_t read_adc_value(void) {
    // Start the conversion process
    ADC1_StartConversion(); 

    // Wait until the end of conversion (EOC) flag is set
    while(ADC1_GetFlagStatus(ADC1_FLAG_EOC) == FALSE);

    // Read the 10-bit value (clears the EOC flag automatically when the data registers are accessed correctly)
    value = ADC1_GetConversionValue(); 

    ADC1_ClearFlag(ADC1_FLAG_EOC); // Explicitly clear the flag if needed, depending on read method and mode

    return value;
}
11 REPLIES 11
Peter BENSCH
ST Employee

The column advances because the HD44780‑compatible LCD controller automatically moves the cursor after each character write, not because Lcd_Set_Cursor() is called multiple times. Therefore, d1, d2, d3 and d4 appear in consecutive columns (6, 7, 8, 9) even though the cursor is explicitly set only once in the code.

The ADC function is unrelated; it merely provides the data that you later display.

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
ankhola
Associate III

Thanks Peter.