sábado, 26 de septiembre de 2009

SDCC Ejemplo 5 USART

Bueno aqui otro ejemplo que acabo de terminar sencillo n_n.


Para mandarle unos comando al pic desde python:

import serial

RS232 = serial.Serial('/dev/ttyUSB0', 1200, timeout=1)

print RS232.portstr
RS232.write(raw_input(':'))

try:
dato=ord(RS232.read())
print dato
dato=ord(RS232.read())
print dato
except:
print "No se recibio ningun dato"

RS232.close()


Programa en C para el pic, utiliza interrupciones para recibir los datos n_n.


#include <pic16f873a.h>

typedef unsigned int word;
word at 0x2007 CONFIG = _HS_OSC & _WDT_OFF & _LVP_OFF &_DEBUG_OFF & _CPD_OFF &_PWRTE_ON &_BODEN_ON;


unsigned char Serial_RX(void);
void Serial_TX(unsigned char);

void isr() interrupt 0 {
unsigned char aux;
if (RCIF==1){
switch(RCREG){
case 'A':RB0=1;break;
case 'B':RB0=0;break;
}
}
Serial_TX('K');
RCIF=0;
}

void main() {
TRISB=0x0;
TRISC=0b10000000;

TXSTA=0b00100000;
SPBRG=255;
PIE1=0b00100000;
RCSTA=0b10010000;
INTCON=0b11000000;

PORTB=0x0;
while(1);
}

unsigned char Serial_RX(){
while(RCIF==0);
return RCREG;
}

void Serial_TX(unsigned char a){
TXREG=a;
while (TRMT==0);
}

jueves, 24 de septiembre de 2009

SDCC Ejemplo 4 ADC

typedef unsigned int word;
word at 0x2007 CONFIG = _HS_OSC & _WDT_OFF & _PWRTE_ON & _BODEN_ON & _LVP_OFF & _CPD_OFF & _WRT_OFF & _DEBUG_OFF & _CP_OFF;


int LeerADC(int);

void main() {
unsigned int aux;
ADCON1=0b00000001; // Vref+ AN3
TRISA=0b11111111;
TRISB=0b00000000;
while(1){
aux = LeerADC(1);
PORTB = aux;
}
}


int LeerADC (int c){
switch(c) {
case 1:
ADCON0=0b01000001; //RA0 AN0
break;
case 2:
ADCON0=0b01001001; //RA1 AN1
break;
case 3:
ADCON0=0b01010001; //RA2 AN2
break;
case 4:
ADCON0=0b01100001; //RA5 AN4
break;
}
GO=1;
while(GO==0);
return ADRESH;
}

SDCC Ejemplo 3 Tmr0

/*Recuerden esto se genero con Piklab pongan el include segun su PIC, 20Mhz*/


typedef unsigned int word;
word at 0x2007 CONFIG = _HS_OSC & _WDT_OFF & _LVP_OFF &_DEBUG_OFF & _CPD_OFF &_PWRTE_ON &_BODEN_ON;

void delay_1ms(void);
void delay_100ms(void);

void main() {
int a;
TRISB= 0b00000000;
INTCON=0b00000000;

T0CS = 0;
TMR0 = 0;
PSA = 0;

while(1){
PORTB=0b11110000;
delay_100ms();
PORTB=0b11100000;
delay_100ms();
PORTB=0b11000000;
delay_100ms();
PORTB=0b10000000;
delay_100ms();
}
}
void delay_100ms(){
int a;
for (a=0;a<=100;a++){
delay_1ms();
}
}

void delay_1ms()
{
PS2=1; PS1=0; PS0=0;
T0IF = 0;
TMR0=100;
while(T0IF==0);
}

lunes, 21 de septiembre de 2009

SDCC Ejemplo 2 Interrupciones RB7-RB4

No se olviden poner el include segun su pic, este ejemplo trata sobre las interrupciones del portb.


void isr() interrupt 0 {
if (RBIF == 1){
if (RB7 == 0){
RB0=1;
}else if (RB6==0){
RB0=0;
}
}
RBIF=0;
}

void main() {
TRISB = 0b11110000;
/*Cambios en RB0-RB4*/
INTCON = 0b10001000;
OPTION_REG = 0b00000111;

PORTB=0;
while(1){
}
}

domingo, 20 de septiembre de 2009

SDCC Ejemplo 1

Falta el include que seria para el pic que tengamos en mi caso un 16f873 con un cristal de 20Mhz - pic16f873a.h todo desde piklab n_n

/* ----------------------------------------------------------------------- */
/* Bits de configuración: adapte los parámetros a su necesidad */
typedef unsigned int word;
word at 0x2007 CONFIG = _HS_OSC & _WDT_OFF & _LVP_OFF &_DEBUG_OFF & _CPD_OFF &_PWRTE_ON &_BODEN_ON;


void isr() interrupt 0 { /* rutina de servicio de interrupciones */
/* << agregue el código de interrupción >> */
}

int i;

void retardo (int);

void main() {

TRISA = 0b11111111;
TRISB = 0b00000000;

while(1) {
PORTB = i;
retardo(250);
i++;
}
}


void retardo(int c)
{
unsigned int a,b;
for (b=1;b<=c;b++){
for (a = 1; a <= 1000; a++);
}
}

gpsim -p p16f873a -s Ejemplo.cod

IRC

#freenode->#usljujuy

Seguidores

Eventos n_n

Tira Ecol Nano,Bilo y Luca