lunes, 15 de junio de 2009

Segundo intento sdcc (12f675)

Mi segunda prueba, me demostró que aunque el sdcc me evite usar el ensamblador me siento más comodo programando en ensamblador, la costumbre es más fuerte.

En este caso estoy utilizando el conversor de ADC del PIC 12F675 para enviar la lectura de una foto resistencia (LDR) a la PC por medio de RS232

/* ----------------------------------------------------------------------- */
/* Template source file generated by piklab */
#include

/* ----------------------------------------------------------------------- */
/* Bits de configuración: adapte los parámetros a su necesidad */
typedef unsigned int word;
word at 0x2007 CONFIG = _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _BODEN_OFF & _CP_OFF & _CPD_OFF;

unsigned char Count,Temp,SerBuf;

#define Tx 1
#define Rx 2

void baud(unsigned char baudrate)
{
_asm
baud:
movlw D'16'
movwf _Count
baud1: decfsz _Count,F
goto baud1

half_baud:
movlw D'16'
movwf _Count
hbaud1: decfsz _Count,F
goto hbaud1
_endasm;
}

void recv_suart()
{
_asm
inch_n: btfsc GPIO,Rx
goto inch_n
movlw 8
movwf _Temp
clrf _SerBuf
call half_baud
;btfsc GPIO,Rx
;goto inch_n
inch_n1:
call baud
bcf STATUS,0
rrf _SerBuf,F
btfsc GPIO,Rx
bsf _SerBuf,7
decfsz _Temp,F
goto inch_n1
call baud
_endasm;
}

void send_suart()
{
_asm
outch_n:
;movwf _SerBuf
movlw 8
movwf _Temp
bcf GPIO,Tx
call baud
outch_n1:
rrf _SerBuf,F
btfss STATUS,0
bcf GPIO,Tx
btfsc STATUS,0
bsf GPIO,Tx
call baud
decfsz _Temp,F
goto outch_n1

rrf _SerBuf,F
bsf GPIO,Tx
call baud
call baud
_endasm;

}
void on_adc() {
_asm
movlw 0x01
movwf ADCON0
_endasm;
}

void off_adc() {
_asm
clrf ADCON0
_endasm;
}

void read_adc() {
_asm
bsf ADCON0,1
waitfc: btfsc ADCON0,1
goto waitfc
_endasm;
}
void initPIC()
{
/*Poner a Ceros el Puerto*/
_asm
BANKSEL GPIO
movlw 0x02
movwf GPIO
movlw 0x07
movwf CMCON
BANKSEL ANSEL
movlw 0x11
movwf ANSEL
movlw 0x0d
movwf TRISIO
BANKSEL GPIO
_endasm;
}

/*Rutina de Retardo*/
void delay(int ret) {
int r;
for(r=0;r<=ret;r++) { } } void main() { initPIC(); SerBuf=0x70; send_suart(); SerBuf=0x69; send_suart(); SerBuf=0x63; send_suart(); SerBuf=0x31; send_suart(); SerBuf=0x32; send_suart(); SerBuf=0x66; send_suart(); SerBuf=0x36; send_suart(); SerBuf=0x37; send_suart(); SerBuf=0x35; send_suart(); SerBuf=0x0D; send_suart(); recv_suart(); send_suart(); while(1){ on_adc(); read_adc(); off_adc(); SerBuf=ADRESH; send_suart(); GP5=1; delay(20000000); GP5=0; delay(20000000); } }
Que es lo que hago:

1.- Envió a la PC pic12f675
2.- Espero a recibir cualquier caracter para iniciar las conversiones
3.- Después de hacer una lectura y enviar el resultado a la PC prendo y apago un led

Necesito más práctica para abandonar el uso del ensamblador cuando use el sdcc.

lunes, 1 de junio de 2009

Mi intento de aprender sdcc para PICs

Con el Piklab en Linux me decidí a aprender a usar el sdcc, de lo cual resultó esta pequeña obra de arte (si como no)


/* ----------------------------------------------------------------------- */
/* Template source file generated by piklab */
#include

/* ----------------------------------------------------------------------- */
/* Bits de configuración: adapte los parámetros a su necesidad */
typedef unsigned int word;
word at 0x2007 CONFIG = _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _BODEN_OFF & _CP_OFF & _CPD_OFF;


void initPIC()
{
/*Poner a Ceros el Puerto*/
_asm
BANKSEL GPIO
clrf GPIO
movlw 0x07
movwf CMCON
BANKSEL ANSEL
CLRF ANSEL
movlw 0x0c
movwf TRISIO
BANKSEL GPIO
_endasm;
}

/*Rutina de Retardo*/
void delay(int ret) {
int r;
for(r=0;r<=ret;r++) { } }

void main() {
initPIC();
while(1){
GP5=1;
delay(10000);
GP5=0;
delay(10000);
}
}


Lo que quería aprender hacerlo en C, pero le terminé metiendo ensamblador para la configuración de los puertos, no puedo negar mis origenes.