You are on page 1of 4

Lectia 3 cu count, n

/***********************************************
main.c
Tutorial Lectia 3
Utilizarea tastaturii
Hardware: KT458-V0 (PIC18F4520)
Nucleu: K5420 V1.0
Adresa de start Firmware: 0x400
Resurse utilizate:
RA4 - intrare tasta DOWN
RC0 - intrare tasta LEFT
RC1 - intrare tasta RIGHT
RC2 - intrare tasta UP
RC5 - iesire LCD backlight
***********************************************/
/***********************************************
Fisiere incluse
***********************************************/
#include <p18cxxx.h>
/***********************************************
Definitii
***********************************************/
#define BKL LATCbits.LATC5 // Comanda backlight
#define DOWN PORTAbits.RA4 // Tasta DOWN
#define LEFT PORTCbits.RC0 // Tasta LEFT
#define RIGHT PORTCbits.RC1 // Tasta RIGHT
#define UP PORTCbits.RC2 // Tasta UP
// Macro-uri pentru comanda backlight
#define BKL_Off() BKL = 0
#define BKL_On() BKL = 1
/***********************************************
Prototipuri
***********************************************/
void high_isr(void);
void low_isr(void);
/***********************************************
Redirectari intreruperi
***********************************************/
#pragma code high_vector = 0x408
void interrupt_at_high_vector(void)
{
_asm
goto high_isr
_endasm
}//end interrupt_at_high_vector
#pragma code low_vector = 0x418
void interrupt_at_low_vector(void)
{
_asm
goto low_isr
_endasm
}//end interrupt_at_low_vector
/***********************************************
Rutine de tratare a intreruperilor (ISR)
***********************************************/
#pragma code
#pragma interrupt high_isr
void high_isr(void)

{
}//end high_isr
#pragma interruptlow low_isr
void low_isr(void)
{
}//end low_isr
/***********************************************
Functia main
***********************************************/
#pragma code
void main(void)
{
unsigned int count,n;
count=0;
n=0;
// Initializare pin interfata
LATAbits.LATA4 = 1; // Initializare PORT LATCH
TRISAbits.TRISA4 = 1; // RA4 intrare
LATC = 0b11011111; // Initializare PORT LATCH
TRISC = 0b11011111; // RC5 - iesire, restul intrari
while(1)
{
n++;
if(n==0)
{
n=count;
BKL=!BKL;
}
if(!UP) // Test tasta UP apasata
{
while(!UP);
if(count<57600)
count=count+2000;
}
if(!DOWN) // Test tasta DOWN apasata
{
while(!DOWN);
if(count>=2000)
count=count-2000;
}
if(!RIGHT) // Test tasta RIGHT apasata
Reset(); // Reset
}//end while
}//end main
/***********************************************
End Of File main.c
***********************************************/
Lectia 3 cu n, PWM
/***********************************************
main.c
Tutorial Lectia 3
Utilizarea tastaturii
Hardware: KT458-V0 (PIC18F4520)
Nucleu: K5420 V1.0
Adresa de start Firmware: 0x400
Resurse utilizate:
RA4 - intrare tasta DOWN
RC0 - intrare tasta LEFT
RC1 - intrare tasta RIGHT

RC2 - intrare tasta UP


RC5 - iesire LCD backlight
***********************************************/
/***********************************************
Fisiere incluse
***********************************************/
#include <p18cxxx.h>
/***********************************************
Definitii
***********************************************/
#define BKL LATCbits.LATC5 // Comanda backlight
#define DOWN PORTAbits.RA4 // Tasta DOWN
#define LEFT PORTCbits.RC0 // Tasta LEFT
#define RIGHT PORTCbits.RC1 // Tasta RIGHT
#define UP PORTCbits.RC2 // Tasta UP
// Macro-uri pentru comanda backlight
#define BKL_Off() BKL = 0
#define BKL_On() BKL = 1
#define RELOAD 57600
/***********************************************
Prototipuri
***********************************************/
void high_isr(void);
void low_isr(void);
/***********************************************
Redirectari intreruperi
***********************************************/
#pragma code high_vector = 0x408
void interrupt_at_high_vector(void)
{
_asm
goto high_isr
_endasm
}//end interrupt_at_high_vector
#pragma code low_vector = 0x418
void interrupt_at_low_vector(void)
{
_asm
goto low_isr
_endasm
}//end interrupt_at_low_vector
/***********************************************
Rutine de tratare a intreruperilor (ISR)
***********************************************/
#pragma code
#pragma interrupt high_isr
void high_isr(void)
{
}//end high_isr
#pragma interruptlow low_isr
void low_isr(void)
{
}//end low_isr
/***********************************************
Functia main
***********************************************/
#pragma code
void main(void)
{
unsigned int pwm,n;

n=0;
pwm=0;
// Initializare pin interfata
LATAbits.LATA4 = 1; // Initializare PORT LATCH
TRISAbits.TRISA4 = 1; // RA4 intrare
LATC = 0b11011111; // Initializare PORT LATCH
TRISC = 0b11011111; // RC5 - iesire, restul intrari
while(1)
{
n++;
if(n==0)
{
n=RELOAD;
}
if(!UP) // Test tasta UP apasata
{
while(!UP);
if(pwm<=1535)
pwm=pwm+307;
}
if(!DOWN) // Test tasta DOWN apasata
{
while(!DOWN);
if(pwm>=307)
pwm=pwm-307;
}
if(n<RELOAD+pwm)
BKL=1;
else
BKL=0;
if(!RIGHT) // Test tasta RIGHT apasata
Reset(); // Reset
}//end while
}//end main
/***********************************************
End Of File main.c
***********************************************/

You might also like