Showing posts with label MikroC Tutorials. Show all posts
Showing posts with label MikroC Tutorials. Show all posts

Sunday, January 3, 2010

PIC Microcontroller Programming Tutorials - Part 1

See the basics of using MikroC compiler and Oshonsoft PIC simulator.



Proceed to the next part >>

Thursday, January 15, 2009

PIC16F877 All Led Blinking Program

Have you ever tried the Hello World of Embedded programming ,well I am talking about the LED Blinking program .Well if you have worked with PIC microcontrollers most probably it must have been your first program . My guess is you must have tried it in any 1 one of the pins .(I am talking all this assuming that you are either using PIC16F877,PIC16F877A or PIC16F874A they are all pretty much the same ) .


Only today when I was testing the PIC16f877/877A/874A Development board that I developed I was stuck up with a peculiar problem .I was trying LED blinking program in all the ports simultaneously .And everything worked fine except for RA4 .The LED connected to it didnt blink .It is the 6th pin and remember it iis an ADC outcast :P .Well being in PORTA where the rest of the pins have a inbuilt ADC this pin doesnt have one so I consider it as so .But it serves as a Vref to the comparator .

I wrote this simple program to make all the led's blink in My dev board .

void main (){
PORTA = 0; // Initialize PORTA
PORTB = 0X00 ;
PORTC = 0X00 ;
PORTD = 0X00 ;
PORTE = 0X00 ;
TRISA = 0x00; // Configure PORTA as output
TRISB = 0X00 ;
TRISC = 0X00 ;
TRISD = 0X00 ;
TRISE = 0X00 ;

while(1){
PORTA = 0XFF ;
PORTB = 0XFF ;
PORTC = 0XFF ;
PORTD = 0XFF ;
PORTE = 0Xff ;
Delay_ms(500) ;
PORTA = 0X00 ;
PORTB = 0X00 ;
PORTC = 0X00 ;
PORTD = 0X00 ;
PORTE = 0X00 ;
Delay_ms(500) ;
}
}

If you think this will work , you are in for a surprise .Check out the next post for the answer .


Friday, January 2, 2009

MikroC Code for reading and writing data into EEPROM

<---Previous Tutorial

Here is an example program showing how to read and write into EEPROM ,This program first writes 0x80 into the address 0x00 and the reads and displays it in portb .

unsigned short data = 0x80,address=0x00;

void main() {
PORTB = 0x00;
TRISB = 0x00;

EEprom_Write(address,data); //writes 0x80 into the location 0x00
Delay_ms(20); //insert 20 ms delay between every read and write cycle
PORTB = Eeprom_Read(address); //reads the data from 0x00 and send it to portb
Delay_ms(500);
}

Sunday, December 14, 2008

PIC16F877A EEPROM Tutorials

There is a inbuilt EEPROM in most of the PIC microcontroller series .PIC16F877A has 256 bytes of EEPROM inside it .So you can use it to store data that you might need on a permanent basis and you can read it back from it .The picture below shows the representation of the memory in the EEPROM .



For instance in the above picture I have entered 0x01 in the location 0x88 .
0x01 is the data and 0x88 is the address where the data is stored .I guess you
got the system .

Now let is look at the mikroc specifics of the EEPROM library .There are two functions in this library to accomplish the task

  • Eeprom_Read
  • Eeprom_Write

Eeprom_Read function returns an integer and takes a parameter of the address from which the data has to be fetched .The following is the syntax .

unsigned short Eeprom_Read(unsigned int address);

Eeprom_Write takes two parameters the first one is the address and the second one is the data .The syntax is given below .

void Eeprom_Write(unsigned int address, unsigned short data);

You can use these two functions to read and write data from the pic EEPROM .I will show you the actual working code in the next post .

Next Post ---->

Saturday, December 13, 2008

mikroc program pulse width modulation

<---Previous Tutorial

Now lets see how to write a code using the mikroc compiler to control the pwm module .
For your convenience MikroC provides PWM library and there are 4 functions present in

  • Pwm_Init
  • Pwm_Change_Duty
  • Pwm_Start
  • Pwm_Stop
The pwm_init takes a arguement of frequency in hertz

To Initialize a PWM module at 5KHz do this :Pwm_Init(5000);

Pwm_Start and Pwm_Stop are self explanatory I guess .These functions doesnt take any arguements .

Pwm_Change_Duty Changes PWM duty ratio. Parameter duty takes values from 0 to 255, where 0 is 0%, 127 is 50%, and 255 is 100% duty ratio. Other specific values for duty ratio can be calculated as (Percent*255)/100.

Take a look at the following simple program

void main (){
portb = 0x00 ; //clear portb
TRISB = 0xFF ; //Set PortB as input
PORTC = 0x00; // clear PORTC 
TRISC = 0; // PORTC is output
Pwm1_Init(5000); // Initialize PWM modulen set frequency as 5000
Pwm1_Start(); //Start the pwm module
while(1){
if (portb==0x80) //if rc7 is high
Pwm_Change_Duty(127); //set the duty cycle as 50%

}
}

This program creates a pwm on pin17 of PIC16F877A .

Hope you found this tutorial useful .If you have any doubts about the program .Leave a comment .