file with mr ndlovu
file with mr ndlovu
h>
#include <stdint.h>
// Configuration word
#pragma config OSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRT = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOR = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial
Programming Enable bit (RB3/PGM pin has digital I/O function; HV on MCLR must be
used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data
EEPROM code protection off)
//#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write
protection off; all program memory may be written to by EECON control)
//#pragma config CP = OFF // Flash Program Memory Code Protection bit
(Code protection off)
void LCD_init() {
TRISC = 0x00; // PORTC as Output Port
__delay_ms(20);
LCD_command(0x02); // Initialize Lcd in 4-bit mode
LCD_command(0x28); // 2 lines, 5x7 matrix
LCD_command(0x0c); // Display On, Cursor Off, Blink Off
LCD_command(0x06); // Increment Cursor (Right)
LCD_command(0x01); // Clear Display
__delay_ms(2);
}
void LCD_clear() {
LCD_command(0x01); // Clear Display
__delay_ms(2);
}
void ADC_Init() {
TRISA0 = 1; // RA0 as Input pin
ADCON0 = 0b00000001;// A/D Converter Module is enabled
ADCON1 = 0b00000000;// A/D Port Configuration: All ports are Analog Input
}
void main() {
unsigned int adc_result;
ADC_Init();
LCD_init();
LCD_clear();
LCD_set_cursor(1, 1);
LCD_data('P');
LCD_data('o');
LCD_data('t');
LCD_data(':');
while(1) {
if(RB0 == 1) { // If zero crossing detected
__delay_us(10); // Debouncing delay
if(RB0 == 1) {
RD0 = 1; // Thyristor ON
}
}
adc_result = ADC_Read(0); // Read ADC value from potentiometer
LCD_set_cursor(1, 6);
LCD_data((adc_result / 1000) + '0');
LCD_data(((adc_result % 1000) / 100) + '0');
LCD_data(((adc_result % 100) / 10) + '0');
LCD_data((adc_result % 10) + '0');
}
}