Code Breakdown:
1. Preprocessor Directives:
#include <pic.h>: Includes the header filepic.h, which contains definitions and declarations specific to PIC microcontrollers.#define _XTAL_FREQ 4000000: Defines a preprocessor constant_XTAL_FREQwith the value 4000000, representing the crystal frequency in Hertz. This value is used by the compiler to calculate delay times and other timing-related calculations.
2. main() Function:
-
Initialization:
PORTC = 0xff;: Sets all bits of PORTC to 1.TRISC = 0x00;: Configures all pins of PORTC as outputs.
-
Main Loop:
-
First For Loop:
- Iterates from 0 to 1 (effectively executing once).
- Sets individual bits of PORTC to 1, one at a time, and delays for 100ms between each bit setting. This creates a sequential lighting effect from left to right.
-
Second For Loop:
- Iterates from 0 to 1 (effectively executing once).
- Sets individual bits of PORTC to 1, starting from the rightmost bit and moving left, and delays for 100ms between each bit setting. This creates a sequential lighting effect from right to left.
-
Explanation:
The code implements a simple LED-based lighting effect on a PIC microcontroller, controlling the individual bits of PORTC to create a sequential lighting pattern.
- The first for loop lights up the LEDs from left to right, starting with the leftmost LED and gradually illuminating the others.
- The second for loop lights up the LEDs from right to left, starting with the rightmost LED and gradually illuminating the others.
#include <htc.h>
#include <math.h>
#include "lcd.h"
//Chip Settings
__CONFIG(1,0x0200);
__CONFIG(2,0X1E1F);
__CONFIG(3,0X8100);
__CONFIG(4,0X00C1);
__CONFIG(5,0XC00F);
//Simple Delay Routine
void Wait(unsigned int delay)
{
for(;delay;delay--)
__delay_us(100);
}
//Function to Initialise the ADC Module
void ADCInit()
{
//We use default value for +/- Vref
//VCFG0=0,VCFG1=0
//That means +Vref = Vdd (5v) and -Vref=GEN
//Port Configuration
//We also use default value here too
//All ANx channels are Analog
/*
ADCON2
*ADC Result Right Justified.
*Acquisition Time = 2TAD
*Conversion Clock = 32 Tosc
*/
ADCON2=0b10001010;
}
//Function to Read given ADC channel (0-13)
unsigned int ADCRead(unsigned char ch)
{
if(ch>13) return 0; //Invalid Channel
ADCON0=0x00;
ADCON0=(ch<<2); //Select ADC Channel
ADON=1; //switch on the adc module
GODONE=1;//Start conversion
while(GODONE); //wait for the conversion to finish
ADON=0; //switch off adc
return ADRES;
}
void main()
{
//Let the LCD Module start up
Wait(100);
//Initialize the LCD Module
LCDInit(LS_BLINK);
//Initialize the ADC Module
ADCInit();
//Clear the Module
LCDClear();
//Write a string at current cursor pos
LCDWriteString("LM35 Test");
LCDWriteStringXY(4,1,"Degree Celcius");
while(1)
{
unsigned int val; //ADC Value
unsigned int t; //Temperature
val=ADCRead(0); //Read Channel 0
t=round(val*0.48876);//Convert to Degree Celcius
LCDWriteIntXY(0,1,t,3);//Prit IT!
Wait(1000);
}
}
#include <math.h>
#include "lcd.h"
//Chip Settings
__CONFIG(1,0x0200);
__CONFIG(2,0X1E1F);
__CONFIG(3,0X8100);
__CONFIG(4,0X00C1);
__CONFIG(5,0XC00F);
//Simple Delay Routine
void Wait(unsigned int delay)
{
for(;delay;delay--)
__delay_us(100);
}
//Function to Initialise the ADC Module
void ADCInit()
{
//We use default value for +/- Vref
//VCFG0=0,VCFG1=0
//That means +Vref = Vdd (5v) and -Vref=GEN
//Port Configuration
//We also use default value here too
//All ANx channels are Analog
/*
ADCON2
*ADC Result Right Justified.
*Acquisition Time = 2TAD
*Conversion Clock = 32 Tosc
*/
ADCON2=0b10001010;
}
//Function to Read given ADC channel (0-13)
unsigned int ADCRead(unsigned char ch)
{
if(ch>13) return 0; //Invalid Channel
ADCON0=0x00;
ADCON0=(ch<<2); //Select ADC Channel
ADON=1; //switch on the adc module
GODONE=1;//Start conversion
while(GODONE); //wait for the conversion to finish
ADON=0; //switch off adc
return ADRES;
}
void main()
{
//Let the LCD Module start up
Wait(100);
//Initialize the LCD Module
LCDInit(LS_BLINK);
//Initialize the ADC Module
ADCInit();
//Clear the Module
LCDClear();
//Write a string at current cursor pos
LCDWriteString("LM35 Test");
LCDWriteStringXY(4,1,"Degree Celcius");
while(1)
{
unsigned int val; //ADC Value
unsigned int t; //Temperature
val=ADCRead(0); //Read Channel 0
t=round(val*0.48876);//Convert to Degree Celcius
LCDWriteIntXY(0,1,t,3);//Prit IT!
Wait(1000);
}
}

Comments
Post a Comment