Example of Circuit |
Exercise 1 :
//ExerciseADC1:Display Voltage Percentage and Temperature
#include <18f4550.h> //Header File
#fuses HS, NOWDT, NOLVP, NOPROTECT //Hardware Configuration
#device ADC=10 //Select 10 bit resolution
#use delay(clock=20M)
#include <lcd.c> //Driver for LCD
#fuses HS, NOWDT, NOLVP, NOPROTECT //Hardware Configuration
#device ADC=10 //Select 10 bit resolution
#use delay(clock=20M)
#include <lcd.c> //Driver for LCD
//Pin Definitions for LCD
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
//Main Function
void main(){
//Variable declaration
int32 value1, voltage, value2, temp, percent;
void main(){
//Variable declaration
int32 value1, voltage, value2, temp, percent;
//initialize LCD
lcd_init();
lcd_init();
//ADC configuration
setup_ADC(ADC_CLOCK_INTERNAL);
setup_ADC_ports(ALL_ANALOG);
setup_ADC(ADC_CLOCK_INTERNAL);
setup_ADC_ports(ALL_ANALOG);
while(true){
//Program Start Here
//Program Start Here
//Voltage
set_adc_channel(0);
value1=read_adc(); //Read analog signal
voltage=value1*5/(1024-1); //Calculate conversion
set_adc_channel(0);
value1=read_adc(); //Read analog signal
voltage=value1*5/(1024-1); //Calculate conversion
//Percentage
percent=value1*100/(1024-1); //Calculate conversion
//Temperature Sensor LM35
set_adc_channel(1);
value2=read_adc(); //Read analog signal
temp=value2*5*100/1023; //Conversion
//Display on LCD
printf(lcd_putc,"\fVoltage = %dV",(int)voltage);
printf(lcd_putc,"\nPercentage=%d%%",(int)percent);
delay_ms(500);
printf(lcd_putc,"\fSuhu=%.2f Degree",(float)temp);
delay_ms(500);
percent=value1*100/(1024-1); //Calculate conversion
//Temperature Sensor LM35
set_adc_channel(1);
value2=read_adc(); //Read analog signal
temp=value2*5*100/1023; //Conversion
//Display on LCD
printf(lcd_putc,"\fVoltage = %dV",(int)voltage);
printf(lcd_putc,"\nPercentage=%d%%",(int)percent);
delay_ms(500);
printf(lcd_putc,"\fSuhu=%.2f Degree",(float)temp);
delay_ms(500);
}
}
}
//**********************************************************
Exercise 2:
//ExerciseADC2: 3 LEDs will turn ON base on temperature
#include <18f4550.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device ADC=10
#use delay(clock=20M)
#include <lcd.c>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device ADC=10
#use delay(clock=20M)
#include <lcd.c>
//Pin Definition
//Pin Definitions for LCD
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
//Pin Definitions for LCD
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
//Pin Definition for LED
#define RED PIN_C0
#define YELLOW PIN_C1
#define GREEN PIN_C2
#define RED PIN_C0
#define YELLOW PIN_C1
#define GREEN PIN_C2
//Main Function
void main(){
int32 value1, temperature;
lcd_init();
//Set PORT C as output
set_tris_c(0x00);
output_c(0x00); //Initialize PORT C
//ADC configuration
setup_ADC(ADC_CLOCK_INTERNAL);
setup_ADC_ports(ALL_ANALOG);
while(true){
//Read Analog Signal
set_adc_channel(1);
value1=read_adc();
temperature=value1*100*5/1023;
if(temperature>=100){
printf(lcd_putc,"\fSuhu Alert \n%.2f Celcius",(float)temperature);
output_high(RED);
output_low(YELLOW);
output_low(GREEN);
delay_ms(500);
}
else if(temperature<=95&&temperature>=25){
printf(lcd_putc,"\fSuhu Normal \n%.2f Celcius",(float)temperature);
output_low(RED);
output_high(YELLOW);
output_low(GREEN);
delay_ms(500);
}
else if(temperature<=20){
printf(lcd_putc,"\fSuhu Low\n%.2f Celcius",(float)temperature);
output_low(RED);
output_low(YELLOW);
output_high(GREEN);
delay_ms(500);
}
}
}
//**********************************************************
Exercise 3:
//ExerciseADC3 : Manipulating LCD for displaying ADC
#include <18f4550.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device ADC=10
#use delay(clock=20M)
#include <lcd.c>
//Pin Definition
//Pin Definitions for LCD
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
//Main Function
void main(){
//Variable declaration
int32 value1, voltage, value2, temp, percent;
//initialize LCD
lcd_init();
//ADC configuration
setup_ADC(ADC_CLOCK_INTERNAL);
setup_ADC_ports(ALL_ANALOG);
while(true){
//Program Start Here
//Voltage
set_adc_channel(0);
value1=read_adc(); //Read analog signal
voltage=value1*5/(1024-1); //Calculate conversion
//Percentage
percent=value1*100/(1024-1); //Calculate conversion
//Temperature Sensor LM35
set_adc_channel(1);
value2=read_adc(); //Read analog signal
temp=value2*5*100/1023; //Conversion
//Display on LCD
printf(lcd_putc,"\fVoltage = %dV",(int)voltage);
delay_ms(500);
printf(lcd_putc,"\fSuhu=%.2f Degree",(float)temp);
delay_ms(500);
}
}
//**********************************************************
Exercise 4:
//ExerciseADC4: Controlling Motor via ADC
#include <18f4550.h>//ExerciseADC4: Controlling Motor via ADC
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device ADC=10
#use delay(clock=20M)
#include <lcd.c>
//Pin Definitions for LCD
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
//Pin Definition for LED
#define Motor1_L PIN_C4
#define Motor1_R PIN_C5
#define Motor2_L PIN_C6
#define Motor2_R PIN_C7
//Main Function
void main(){
int32 value1, voltage;
lcd_init();
//Set PORT C as output
set_tris_c(0x00);
output_c(0x00); //Initialize PORT C
//ADC configuration
setup_ADC(ADC_CLOCK_INTERNAL);
setup_ADC_ports(ALL_ANALOG);
while(true){
//Read Analog Signal
set_adc_channel(0);
value1=read_adc();
voltage=value1*5/1023;
if(voltage>=3){
printf(lcd_putc,"\fVoltage \n%dV",(int)voltage);
output_high(Motor1_L);
output_low(Motor1_R);
output_low(Motor2_L);
output_low(Motor2_R);
delay_ms(500);
}
else if(voltage<=2){
printf(lcd_putc,"\fVoltage \n%dV",(int)voltage);
output_low(Motor1_L);
output_low(Motor1_R);
output_high(Motor2_L);
output_low(Motor2_R);
delay_ms(500);
}
else{
printf(lcd_putc,"\fVoltage \n%dV",(int)voltage);
output_low(Motor1_L);
output_low(Motor1_R);
output_low(Motor2_L);
output_low(Motor2_R);
delay_ms(500);
}
}
}
//**********************************************************
Exercise 5:
//ExerciseADC5: Controlling LED bargraph
#include <18f4550.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device ADC=10
#use delay(clock=20M)
#include <lcd.c>
//Pin Definition
//Pin Definitions for LCD
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
//Main Function
void main(){
int32 value1, voltage;
lcd_init();
//Set PORT B as output
set_tris_b(0x00);
output_b(0x00); //Initialize PORT C
//ADC configuration
setup_ADC(ADC_CLOCK_INTERNAL);
setup_ADC_ports(ALL_ANALOG);
while(true){
//Read Analog Signal
set_adc_channel(0);
value1=read_adc();
voltage=value1*5/1023;
if(voltage==0){
printf(lcd_putc,"\fVoltage \n%dV",(int)voltage);
output_b(0);
delay_ms(500);
}
else if(voltage==1){
printf(lcd_putc,"\fVoltage \n%dV",(int)voltage);
output_b(3);
delay_ms(500);
}
else if(voltage==2){
printf(lcd_putc,"\fVoltage \n%dV",(int)voltage);
output_b(15);
delay_ms(500);
}
else if(voltage==3){
printf(lcd_putc,"\fVoltage \n%dV",(int)voltage);
output_b(31);
delay_ms(500);
}
else if(voltage==4){
printf(lcd_putc,"\fVoltage \n%dV",(int)voltage);
output_b(63);
delay_ms(500);
}
else if(voltage==5){
printf(lcd_putc,"\fVoltage \n%dV",(int)voltage);
output_b(255);
delay_ms(500);
}
}
}
No comments:
Post a Comment