User manual

Table Of Contents
270
mikoC PRO for PIC32
MikroElektronika
Library Example
This code snippet reads analog value from the channel 1 and sends readings as a text over UART1.
Copy Code To Clipboard
// LCD module connections
sbit LCD_RS at LATB2_bit;
sbit LCD_EN at LATB3_bit;
sbit LCD_D4 at LATB4_bit;
sbit LCD_D5 at LATB5_bit;
sbit LCD_D6 at LATB6_bit;
sbit LCD_D7 at LATB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
oat temp;
char txt[20];
// Convert ADC value to Celsius degrees format
oat ADC_to_degC(){
oat result;
result = ADC1_Get_Sample(8); // Read ADC value from AN8 pin
result = ((3.25/1024) * result-.5) / 0.01;
return result;
}
void main() {
CHECON = 0x32;
AD1PCFG = 0xFFF7; // Congure AN8 pin as analog I/O
ADC1_Init(); // Initialize ADC
Delay_100ms();
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
Lcd_Out(1, 1, “ Temperature: “);
while(1) {
temp = ADC_to_degC(); // Convert ADC value to Celsius degrees format
FloatToStr(temp, txt);
Lcd_Chr(2,13,223); // Print degree character, ‘C’ for Centigrades
// Different LCD displays have different char code for degree
Lcd_Chr(2,14,’C’); // If you see greek alpha letter try typing 178 instead of 223
Lcd_Out(2, 5, txt);
Delay_1sec();
}
}