Skip to main content

another knight Rider



Code Breakdown:

1. Preprocessor Directives:

  • #include <pic.h>: Includes the header file pic.h, which contains definitions and declarations specific to PIC microcontrollers.
  • #define _XTAL_FREQ 4000000: Defines a preprocessor constant _XTAL_FREQ with 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);
   }

}

Comments

Popular posts from this blog

PicBasic and PicBasic Pro programming

B ASIC is one of the oldest and one of the easiest pr o g ramming languages to lea r n. Y ou should be a b le to lea r n and pr o g ram in B ASIC in less than an hou r . In this chapte r , w e shall be looking at the principles of pr o g ramming PIC microcontrollers using the PicBasic and PicBasic Pro lan- guages. Both these languages are v e r y similar to the standard B ASIC language but th e y h a v e some modi f ied and some additional inst r uctions speci f ical l y for microcontroller pr o g ramming. Both PicBasic and PicBasic Pro languages h a v e been d e v eloped b y the microEngineering Labs Inc. PicBasic is a l o w e r -cost, simpler language than PicBasic Pro and it is aimed at students and hob b yists. PicBasic Pro is more e xpens i v e, aimed at professionals, and includes additional com- mands for more ad v anced inst r uctions. T a b le g i v es a list of the comparison of PicBasic and PicBasic Pro languages. Before w e pro- ceed to the chapter...

Voltmeter

This code to read an analog temperature sensor, convert the reading to a digital value using the ADC, and display the temperature on a 4-digit 7-segment display. The display is updated periodically based on a timer interrupt. 1.Initialization: Setting up ports, configuring ADC, and Timer0. 2.Infinite Loop: Continuously reads analog input and updates the display. 3.ADC Conversion: Reads analog voltage from a sensor. 4.Voltage Calculation: Converts ADC value to voltage. 5.Digit Extraction: Extracts individual digits from the temperature/voltage value. 6.Display Function: Displays the digits on a 4-digit 7-segment display with slight delays. --------------------------------------------- #include <pic.h> // Include the header file for PIC microcontroller family. unsigned char PORTB_value[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; // Array containing values for 7-segment display. unsigned int a2d_value,temp; // Variables for analog to digital conversion and temporary sto...

timer_500mS

Code Breakdown: 1. Preprocessor Directives: #include <pic.h> : Includes the header file pic.h , which contains definitions and declarations specific to PIC microcontrollers. 2. Variable Declarations: unsigned char count = 0; : Declares an unsigned character variable count and initializes it to 0. This variable is used to keep track of the number of 50ms intervals that have elapsed. 3. main() Function: Initialization: TRISC = 0; : Configures PORTC as output pins. PORTC = 0; : Clears the output pins on PORTC. T0CS = 0; : Selects the internal clock source (Fosc/4) for TMR0. PSA = 0; : Assigns the prescaler to TMR0. PS2 = 1; PS1 = 1; PS0 = 1; : Sets the prescaler to 256, providing a timer tick every 50 milliseconds. TMR0 = 61; : Initializes TMR0 with the value 61 to generate a 50ms delay. RC0 = 1; : Sets the output on RC0. Main Loop: while (1) { ... } : The main loop continues indefinitely. Interrupt Handling: if (T0IF) { ... } : Checks if the TMR0 interrupt ...