quarta-feira, 4 de junho de 2008

#include no CCS

Caros, recentemente numa lista que participo houve uma pergunta sobre como dividir um programa gigante, uma verdadeira tripa de código em blocos *.c ... bom ... a resposta está aí abaixo... Good Luck...

PS: Como estou sem saco pra identar o código aqui no blog... SE VIRA NEGÃO!


Exemplo.c
=========================================
#include<16f877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT,BROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
//#use i2c(master, sda=PIN_C4, scl=PIN_C3)
#byte port_a=5
#byte port_b=6
#byte port_d=8

#include

// Prototypes
void hw_setup(void);

// Variables
byte mychar;

// Main Function
void main(void) {
hw_setup();
while(true) {
if(kbd_hasKey()) {
mychar=kbd_read();
}
}
}

void hw_setup(void) {
set_tris_a(0);
set_tris_b(0);
set_tris_d(0b11110000);
port_b=0;
port_d=0;
//init_ext_eeprom();
kbd_init();
//lcd_init();
//ds1307_init(DS1307_OUT_ON_DISABLED_HIHG | DS1307_OUT_ENABLED | DS1307_OUT_1_Z);
}
=========================================

keyb3x4.c
=========================================
// keyb3x4.c
// Change these pins to fit your own board.

#define KBD_C1 PIN_B0
#define KBD_C2 PIN_B1
#define KBD_C3 PIN_B2

#define KBD_L1 PIN_B3
#define KBD_L2 PIN_B4
#define KBD_L3 PIN_B5
#define KBD_L4 PIN_B6

char detected;

char verifyLine(int mapL) {
int line = 0;
if(bit_test(mapL,0)) {
line = 1;
output_high(KBD_L1);
} else {
output_low(KBD_L1);
}
if(bit_test(mapL,1)) {
line = 2;
output_high(KBD_L2);
} else {
output_low(KBD_L2);
}
if(bit_test(mapL,2)) {
line = 3;
output_high(KBD_L3);
} else {
output_low(KBD_L3);
}
if(bit_test(mapL,3)) {
line = 4;
output_high(KBD_L4);
} else {
output_low(KBD_L4);
}
if(input(KBD_C1)==1) {
while(input(KBD_C1)==1) delay_ms(10);
if(line==1) return('1');
if(line==2) return('4');
if(line==3) return('7');
if(line==4) return('*');
}
if(input(KBD_C2)==1) {
while(input(KBD_C2)==1) delay_ms(10);
if(line==1) return('2');
if(line==2) return('5');
if(line==3) return('8');
if(line==4) return('0');
}
if(input(KBD_C3)==1) {
while(input(KBD_C3)==1) delay_ms(10);
if(line==1) return('3');
if(line==2) return('6');
if(line==3) return('9');
if(line==4) return('#');
}
return('\0');
}

void detectKey(void) {
int t;
int mapL;
int mapC;

mapL = 0x0000001;
for (t=0; t<4; t++) {
mapC = verifyLine(mapL);
mapL = mapL << 1;
if(mapC != '\0') {
detected = mapC;
return;
}
}
}

void kbd_init(void) {
detected='\0';
}

int kbd_hasKey(void) {
detectKey();
if(detected != '\0') return(1);
return(0);
}

char kbd_read() {
char toReturn;
toReturn = detected;
detected='\0';
return(toReturn);
}
=========================================

E agora... como eu sempre digo: mato a cobra e mostro o pau!!!

Eis o output do CCS...
=========================================
Clean: Deleting intermediary and output files.
Clean: Deleted file "Exemplo.ERR".
Clean Warning: File "C:\test\Exemplo\Exemplo.o" doesn't exist.
Clean: Done.
Executing: "C:\Program Files\PICC\Ccsc.exe" "Exemplo.c" +FM +DF +LN +T -A +M +Z +Y=9 +EA
Memory usage: ROM=3% RAM=2% - 4%
0 Errors, 0 Warnings.
Loaded C:\test\GustavoExemplo\Exemplo.cof.
BUILD SUCCEEDED: Wed Jun 04 21:56:32 2008
=========================================

Nenhum comentário:

Postar um comentário