0% found this document useful (0 votes)
0 views

file with mr ndlovu

The document is a C program for a microcontroller that interfaces with an LCD and an ADC to display the value of a potentiometer. It includes configuration settings, initialization functions for the LCD and ADC, and a main loop that reads the ADC value and displays it on the LCD when a zero crossing is detected. The program utilizes specific pins for control and data communication with the LCD and handles input from a button to control a thyristor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

file with mr ndlovu

The document is a C program for a microcontroller that interfaces with an LCD and an ADC to display the value of a potentiometer. It includes configuration settings, initialization functions for the LCD and ADC, and a main loop that reads the ADC value and displays it on the LCD when a zero crossing is detected. The program utilizes specific pins for control and data communication with the LCD and handles input from a button to control a thyristor.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <xc.

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)

#define _XTAL_FREQ 20000000 // 20 MHz crystal frequency

// LCD Control pins


#define RS PORTCbits.RC0
#define EN PORTCbits.RC1

// LCD Data pins


#define D4 PORTCbits.RC4
#define D5 PORTCbits.RC5
#define D6 PORTCbits.RC6
#define D7 PORTCbits.RC7

void LCD_command(unsigned char cmd) {


RS = 0; // Selected Command Register
PORTC = (PORTC & 0x0F) | (cmd & 0xF0); // Send higher nibble
EN = 1; // Enable pulse
__delay_ms(4);
EN = 0;
PORTC = (PORTC & 0x0F) | ((cmd << 4) & 0xF0); // Send lower nibble
EN = 1; // Enable pulse
__delay_ms(4);
EN = 0;
}

void LCD_data(unsigned char dat) {


RS = 1; // Selected Data Register
PORTC = (PORTC & 0x0F) | (dat & 0xF0); // Send higher nibble
EN = 1; // Enable pulse
__delay_ms(4);
EN = 0;
PORTC = (PORTC & 0x0F) | ((dat << 4) & 0xF0); // Send lower nibble
EN = 1; // Enable pulse
__delay_ms(4);
EN = 0;
}

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 LCD_set_cursor(unsigned char row, unsigned char column) {


unsigned char temp, z, y;
if (row == 1) {
temp = 0x80 + column - 1;
z = temp >> 4;
y = temp & 0x0F;
LCD_command(z);
LCD_command(y);
} else if (row == 2) {
temp = 0xC0 + column - 1;
z = temp >> 4;
y = temp & 0x0F;
LCD_command(z);
LCD_command(y);
}
}

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
}

uint16_t ADC_Read(uint8_t channel) {


ADCON0 &= 0xC5; // Clearing channel selection bits
ADCON0 |= (channel << 3); // Setting channel selection bits
ADCON0bits.GO = 1; // Initializing A/D conversion
while (ADCON0bits.GO); // Waiting for conversion to complete
return ((ADRESH << 8) + ADRESL); // Return result
}

void main() {
unsigned int adc_result;

TRISB0 = 1; // RB0 as Input pin


TRISD0 = 0; // RD0 as Output pin

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');
}
}

You might also like