|
Så er tiden kommet til å ta skrittet ut av PC-box programmering, og inn i den lille store verden av PIC programmering.
 
Desverre har det ikke vært mulig å finne stabile konfigurasjoner eller applikasjoner i Linuxverden til å lære seg dette. Har vært
innom det meste inkludert PIKDEV, PIKLAB, GPUTILS og GPSIM, samt SDCC. Men ingen av disse programmene var hverken
stabile nok eller hadde et brukergrensesnitt som var lettfattelig. Med tid og stunder blir det nok lettere og lettere.
Vi har derfor tatt i bruk en gammel IBM ThinkPad med Windows XP. Her har vi installert MikroC og MPLAB
Programmereren vi bruker er PICCOLO, kjøpt av elfa.se
Levende dokument. Blir oppdatert jevnlig.
PIC'en vi skal konsentrere oss om er PIC 16F628. Det er en liten 18-pin ic.
Programmet vi skal lage er en LED-flasher. (oh, no. Ikke enda en!) Programet vi skal bruke til å "brenne" ic'en med, er lastet ned fra produsentens hjemmeside.
Det er uhyre viktig å lese Databladet til IC du skal programmere. Jeg har en del PIC 16F628, som vi skal leke oss litt med her. Databladet finner du her eller her. Les også header-filen som tilhører IC'n. Her finner du masse informasjon.
</code>
/*
Program: main.c
Description: Blinking LED according to push buttons
PIC: 16F628
IDE: MPLAB
Compiler: Hi - Tech C
Date: Oct 2010
Author: Jens Christoffersen
web: www.nerdegutta.org
*/
#include // 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
En takk til gutta på forumet AAC.
For å se programmet 'live'(?) Klikk på linken
|