Hi.
I'm in the beginning of my journey to learn Hi-Tech C, MPLAB, and PIC MCU programming. I've spent some time trying to make "Yet another LED flasher".
I would be very thankful if someone could provide me with some comments on my program.
I've put a lot of comments in, so that I might remember them over time.
I know all the IF-statements in main() could be done with switch - case statement, but I'm still learning.
A small drawback is that the PIC needs to be reset before another bit can be checked for HIGH.
When it come to the TRISA, TRISB, PORTA, PORTB and CMCON I'm a little concerned if I've got it right...
Would be great with some comments.
I'm in the beginning of my journey to learn Hi-Tech C, MPLAB, and PIC MCU programming. I've spent some time trying to make "Yet another LED flasher".
I would be very thankful if someone could provide me with some comments on my program.
I've put a lot of comments in, so that I might remember them over time.
Rich (BB code):
/*
Program: main.c
Description: Blinking LED according to pushed buttons
PIC: 16F628
IDE: MPLAB
Compiler: Hi - Tech C
Date: Oct 2010
Author: Jens Christoffersen
web: www.nerdegutta.org
*/
#include <htc.h> // Header file for PIC 16F6xx
#include "..\delay.c" // Needed for DelayMs() - function
/* Configuration */
__CONFIG (WDTDIS & // Disable watchdog
PWRTEN & // Disable power up timer
MCLREN & // Master Clear Reset enable
BOREN & // Enabale Brown out reset
LVPDIS & // Disable low voltage programming
DATUNPROT & // Unprotect data code
UNPROTECT & // Unprotect the program code
INTIO); // Use internal RC oscillator
/* Functions start here */
void blinkAllLEDS()
{
PORTB = 0b11111111; // Turning all LEDs on
DelayMs(5); // Wait 5 ms
PORTB = 0b00000000; // Turning all LEDs off
DelayMs(5); // Wait 5 ms
} // End blinkAllLEDS
void goLeft()
{
int counterPortB = 1; // Declaring counterPortB as integer variable with the value of 1
while (counterPortB < 255) // While counterPortB is less than 255 do...
{
PORTB = counterPortB; // Sending the value of counterPortB to port b
DelayMs(10); // Wait 10 ms
PORTB = 0; // Turning all bits LOW
DelayMs(10); // Wait 5 ms
counterPortB = counterPortB * 2; // For each run, counterPortB is multiplied by 2
}
counterPortB = 1; // Setting counterPortB to 1
} // End goLeft
void goRight()
{
int counterPortB = 256; // Declaring counterPortB as integer variable with the value of 256
while (counterPortB > 0) // While counterPortB is bigger than 0 do...
{
PORTB = counterPortB; // Sending the value of counterPortB to port b
DelayMs(10); // Wait 10 ms
PORTB = 0; // Turning all bits LOW
DelayMs(10); // Wait 10 ms
counterPortB = counterPortB / 2; // For each run, counterPortB divided by 2
}
counterPortB = 256; // Sett counterPortB to the value of 256
} // End goLeft
void goLeftRight()
{
goLeft();
goRight();
}
/* Start with main program */
void main()
{
TRISA = 0b11111111; // Setting all bits on port a to input
TRISB = 0b00000000; // Setting all bits on port b to output
PORTA = 0b00000000; // Setting all bits on port a to LOW
PORTB = 0b00000000; // Setting all bits on port b to LOW
CMCON = 0x07; // Disabling the analogue comparators
while (1) // Start endless lopp
{
if (RA0 == 1) // Checks if bit RA0 is 1 -> button pressed.
{ // Start if-statement
while (1) // Start while loop
{ // Starting braces
blinkAllLEDS(); // Calling the blinkAllLEDS function
} // End while loop
} // End if-statement
if (RA1 == 1)
{
while (1)
{
goLeft();
}
}
if (RA2 == 1)
{
while (1)
{
goRight();
}
}
if (RA3 == 1)
{
while (1)
{
goLeftRight();
}
}
} // End while-loop
} // End main function
A small drawback is that the PIC needs to be reset before another bit can be checked for HIGH.
When it come to the TRISA, TRISB, PORTA, PORTB and CMCON I'm a little concerned if I've got it right...
Would be great with some comments.