Fórum

Controle remoto PIC...
 
Notifications
Clear all

Controle remoto PIC12F675 compilar codigo fonte no MikroC

4 Posts
2 Users
0 Likes
1,733 Leituras
(@fatevian)
Active Member
Joined: 5 anos ago
Posts: 4
Topic starter  

Boa noite. Estou tentando converter o código fonte do transmissor de 5 canais com pic12f675 postado pelo blog, porém está dando o seguinte erro:

as entradas de GPIO estão sendo lidas como zero exceto GP3, então o código carregado é sempre o 0x05,  não importando a tecla pressionada.

Alguém pode me dar uma dica do que pode estar ocorrendo?

int make8(int, int); //splits int into 2 char

const int serial_number = 0x1727;

const int te = 500; //valor de TE

#define gpio 0x05
#define tris 0x85
#define saida GP0_bit

int _data[4];

//--------------------------------------------------------
//Rotina gerador de tempo piloto de 23 TE sem transmissao
//--------------------------------------------------------
void pilot_period(){
saida = 0;
delay_us(23*te);
}
//--------------------------------------------------------
//Rotina geradora de start bit
//--------------------------------------------------------
void start_bit(){
saida = 1;
delay_us(te);
saida = 0;
}
//--------------------------------------------------------
//Rotina geradora de bit '1'
//--------------------------------------------------------
void bit1(){
saida = 1; //1
delay_us(2*te);
saida = 0;
delay_us(te);
saida = 0;
}
//---------------------------------------------------------
//Rotina gerador de bit '0'
//---------------------------------------------------------
void bit0(){
saida = 1; //0
delay_us(te);
saida = 0;
delay_us(2*te);
saida = 0;
}
//---------------------------------------------------------
//Rotina de envio para saida RF de um byte
//---------------------------------------------------------
void envia(int valor){
int b;
for(b=0; b<8; b++){
if(valor&1){
bit1();
}else{
bit0();
}
valor>>=1; //desloca 1 bit para direita
}
}
//---------------------------------------------------------
//Rotina de envio saida RF de 4 bytes
//---------------------------------------------------------
void transmite(){
int a;
pilot_period(); //23 TE desligado
start_bit(); //bit de inicio

for(a=4; a>0; a--){ //envia 4 bytes
envia(_data[a-1]);
}
}
//----------------------------------------------------------
//Rotina de separar 1 int em 2 char(Tive que fazer, pois não existe no MikroC)
//----------------------------------------------------------
int make8(unsigned int num, unsigned int i){
const unsigned int mask = 0xFF;
unsigned int number;
unsigned char nLow, nHigh;
number=num;
nLow=number&mask; //extrai o nible Low
nHigh=(num>>8)&mask; //extrai o nible High
if(i==0) return nLow;
if(i==1) return nHigh;

}
//---------------------------------------------------------
//Rotina principal
//---------------------------------------------------------

 

void main(){
//RP0_BIT = 0;
GPIO = 0x00;
CMCON = 0x07;
RP0_BIT = 1;
ANSEL = 0x00;
TRISIO = 0x3E;
GPIO = 0x3E;

while(1){

if((gpio&0x3e)!=0x3e){//se alguma tecla foi apertada(essa não funcionou pois retorna falso
//while((GP5_BIT & GP4_BIT & GP3_BIT & GP2_BIT & GP1_BIT) == 1){} //fiz para teste. Funciona, porém GPIO sempre lê 0x05;
_data[0]=make8(serial_number,1);
_data[1]=make8(serial_number,0);
_data[2]=gpio;
_data[3]=_data[0]+_data[1]+_data[2];

transmite();
}
//}
}
}


   
Quote
(@fatevian)
Active Member
Joined: 5 anos ago
Posts: 4
Topic starter  

Boa noite. Após quebrar um pouco a cabeça, consegui compilar tanto o transmissor como o receptor no MikroC,  com algumas poucas modificações pois certas funções não estão presentes no compilador MikroC. Estou compartilhando, caso alguém se interesse. Aproveito para parabenizar o sr. Cláudio Lários, pelo excelente conteúdo de seu Blog.

 

Código Transmissor modificado para compilação no MikroC:

 

//***************************************************************************
//
// Transmissor de 5 canais on/off com pic 12f675
//
//
// Autor: Cláudio Lários Data:10/07/2014
//
//
//
//
// Uso didático apenas.
// Use apenas em conjunto com o rx de 5 canais on/off (ref156)
// Obs. Sujeito a bugs ainda não observados.
//
***************************************************************************
// Código modificado por Fábio T. Vianna para ser compilado no MikroC OK
// 26/9/19
int make8(unsigned int, unsigned int); //splits int into 2 char
void transmite(void);
const int serial_number = 0x89ab;

const int te = 500; //valor de TE

#define gpio 0x05
#define tris 0x85
#define saida GP0_bit //pino 7

int _data[4];

//---------------------------------------------------------
//Rotina principal
//---------------------------------------------------------
void main(){
//RP0_BIT = 0;
GPIO = 0x00;
CMCON = 0x07;
//RP0_BIT = 1;
ANSEL = 0x00;
TRISIO = 0x3E;
GPIO = 0x3D;

while(1){

if((GPIO&0x3e)!=0x3e){//se alguma tecla foi apertada
_data[0]=make8(serial_number,1);
_data[1]=make8(serial_number,0);
_data[2]=GPIO;
_data[3]=_data[0]+_data[1]+_data[2];

transmite();
}
//}
}
}

//--------------------------------------------------------
//Rotina geradora de tempo piloto de 23 TE sem transmissao
//--------------------------------------------------------
void pilot_period(){
saida = 0;
delay_us(23*te);
}
//--------------------------------------------------------
//Rotina geradora de start bit
//--------------------------------------------------------
void start_bit(){
saida = 1;
delay_us(te);
saida = 0;
}
//--------------------------------------------------------
//Rotina geradora de bit '1'
//--------------------------------------------------------
void bit1(){
saida=0; //'1'
delay_us(2*te);
saida=1;
delay_us(te);
saida=0;
}
//---------------------------------------------------------
//Rotina gerador de bit '0'
//---------------------------------------------------------
void bit0(){
saida=0; //'0'
delay_us(te);
saida=1;
delay_us(2*te);
saida=0;
}
//---------------------------------------------------------
//Rotina de envio para saida RF de um byte
//---------------------------------------------------------
void envia(int valor){
int b;
for(b=0; b<8; b++){
if(valor&1){
bit1();
}else{
bit0();
}
valor>>=1;
}
}
//---------------------------------------------------------
//Rotina de envio saida RF de 4 bytes
//---------------------------------------------------------
void transmite(){
int a;
pilot_period(); //23 TE desligado
start_bit(); //bit de inicio

for(a=4; a>0; a--){ //envia 4 bytes
envia(_data[a-1]);
}
}
//----------------------------------------------------------
//Rotina de separar 1 int em 2 char
//----------------------------------------------------------
int make8(unsigned int num, unsigned int i){
const unsigned int mask = 0xFF;
unsigned int number;
unsigned char nLow, nHigh;
number=num;
nLow=number&mask; //extrai o nible Low
nHigh=(num>>8)&mask; //extrai o nible High
if(i==0) return nLow;
if(i==1) return nHigh;

}

 

Código Receptor modificado para compilação no MikroC:

 

//***************************************************************************
//
// Receptor de 5 canais on/off com pic 12f675
//
//
// Autor: Cláudio Lários Data:10/07/2014
// Atualizado em :18/12/2014
//
// 5 Canais configuravéis para modo pulso/retenção e
// modo invertido/normal por pino individual.
// Configure nas definições de usuário e recompile.
//
// Uso didático apenas.
// Use apenas em conjunto com o tx de 5 canais on/off // (ref156b)
// Obs. Sujeito a bugs ainda não observados.
//
***************************************************************************
// Código modificado para ser compilado no mikroC
// Fábio T. Vianna 26/9/19 OK
void init_ports(void);
char Shift_Right(char* address, char bytes, char value);
//DEFINIÇÕES DO USUÁRIO:
//Define o modo de operação das saidas (comente p/ modo pulso e descomente para
// modo 'retenção'):

#define c1_ret
#define c2_ret
#define c3_ret
#define c4_ret
#define c5_ret

//Define se modo normal ou invertido (comente para normal e descomente para modo
// invertido):

// c1_inv
//#define c2_inv
//#define c3_inv
//#define c4_inv
//#define c5_inv

//==============================================================================

//#include <12F675.h>
//#device adc=8
//#use delay(clock=4000000)
//#fuses NOWDT,INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOPUT, BROWNOUT
//#zero_ram

//#byte gpio = 0x05
#define gpio GPIO
//#byte tris = 0x85
#define tris TRISIO

//#bit rfin = 0x05.3
#define rfin GP3_BIT
#define c1 GP0_BIT //pino 7
#define c2 GP1_BIT //pino 6
#define c3 GP2_BIT //pino 5
#define c4 GP4_BIT //pino 3
#define c5 GP5_BIT //pino 2
//#bit c1 = 0x05.0 //pino 7
//#bit c2 = 0x05.1 //pino 6
//#bit c3 = 0x05.2 //pino 5
//#bit c4 = 0x05.4 //pino 3
//#bit c5 = 0x05.5 //pino 2

//int flag_rok,flag_h,fc1,fc2,fc3,fc4,fc5;
bit flag_rok,flag_h,fc1,fc2,fc3,fc4,fc5;

//int lc,hc,qb,cont_c1,cont_c2,cont_c3,cont_c4,cont_c5,aux_seg,aux_seg1;
short lc,hc,qb,cont_c1,cont_c2,cont_c3,cont_c4,cont_c5,aux_seg,aux_seg1;
//int buffer[4];
unsigned char buffer[4];

const int tmax= 80; //80
//const int8 t_inv=10;
const unsigned char t_inv=10;
const int serial_number = 0x89ab; //use a mesma no tx (obrigatório)

//const int8 tempo_seg = 0x07;//0x05 min
const unsigned char tempo_seg = 0x07;//0x05 min

void init_ports(){
#ifndef c1_inv
c1=0;
#else
c1=1;
#endif

#ifndef c2_inv
c2=0;
#else
c2=1;
#endif

#ifndef c3_inv
c3=0;
#else
c3=1;
#endif

#ifndef c4_inv
c4=0;
#else
c4=1;
#endif

#ifndef c5_inv
c5=0;
#else
c5=1;
#endif

}

//==============================================================================
// ZERA REGISTRADORES APÓS RECEPÇÃO DE UM BIT
//==============================================================================
void reset_rx (){ lc=0; hc=0; flag_h=0; }

//==============================================================================
// Rotina de recepção RF
//==============================================================================

void recebe(){

if(rfin&&!flag_rok){ flag_h=1; if(!++hc) --hc; }
else {
if(!flag_h){if(!++lc) --lc;} // incrementa lc e limita 0xff
else {
if(lc>tmax) {reset_rx (); qb=32;}
else { Shift_Right(&buffer[0],4,(lc>hc)) ;
if (!--qb){
qb=32; //salva botões
flag_rok=1;} // há uma recepção no buffer
reset_rx();
}
}
}
delay_us(50);
}

//==============================================================================
// Rotina de atualização de flags no modo retenção
//==============================================================================

void at_cont_ret(){

if(!--cont_c1)
#ifdef c1_ret
fc1=0;
#else
#ifdef c1_inv
c1=1;
#else
c1=0;
#endif
#endif

if(!--cont_c2)
#ifdef c2_ret
fc2=0;
#else
#ifdef c2_inv
c2=1;
#else
c2=0;
#endif
#endif

if(!--cont_c3)
#ifdef c3_ret
fc3=0;
#else
#ifdef c3_inv
c3=1;
#else
c3=0;
#endif
#endif

if(!--cont_c4)
#ifdef c4_ret
fc4=0;
#else
#ifdef c4_inv
c4=1;
#else
c4=0;
#endif
#endif

if(!--cont_c5)
#ifdef c5_ret
fc5=0;
#else
#ifdef c5_inv
c5=1;
#else
c5=0;
#endif
#endif

}
//============================================================================
// Rotina shift_righ(nao tem no MikroC) FABIO 25/9/19
//============================================================================
char Shift_Right(char* address, char bytes, char value) {
char oldch, ch, carry;

// set carry for the highest (last) byte:
carry = 0;
if (value) // if nonzero:
carry.B0 = 1; // 1 to be shifted in to new MSB
address += bytes - 1; // points to last byte
for ( ; bytes ; --address, --bytes ) { // loop 'bytes' times

oldch = ch = *address; // get and save the actual byte
ch >>= 1; // right shifted one times and ch.B7 = 0
if (carry.B0)
ch.B7 = 1;
*address = ch; // write it back to the array
// set carry for the next byte:
carry = 0;
if (oldch.B0)
carry.B0 = 1; // carry = 1

} // end for
return carry; // out-shifted bit (original LSB), 0 or 1
}

//----------------------------------------------------------
//Rotina de separar 1 int em 2 char
//----------------------------------------------------------
int make8(unsigned int num, unsigned int i){
const unsigned int mask = 0xFF;
unsigned int number;
unsigned char nLow, nHigh;
number=num;
nLow=number&mask; //extrai o nible Low
nHigh=(num>>8)&mask; //extrai o nible High
if(i==0) return nLow;
if(i==1) return nHigh;

}
//==============================================================================
// Rotina Principal
//==============================================================================

void main() {

//setup_adc_ports(NO_ANALOGS|VSS_VDD);
//setup_adc(ADC_OFF);
//setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
//setup_timer_1(T1_DISABLED);
//setup_comparator(NC_NC_NC_NC);
//setup_vref(FALSE);
CMCON = 0x07;
//RP0_BIT = 1;
ANSEL = 0x00;
//TRISIO = 0x3E;
//GPIO = 0x3E;
tris=0b001000;
init_ports();
flag_rok=0;
aux_seg1=tempo_seg; //inicializa variavel <acrescentar

//==============================================================================
// Loop Principal
//==============================================================================
while(1){
//int8 conf,aux,aux_ret;
unsigned char conf,aux,aux_ret;
//int1 flag_acionar,flag_1rec; //<acrescentar
bit flag_acionar,flag_1rec; //<acrescentar

if(!--aux_ret){aux_ret=250; at_cont_ret();}
if(!--aux_seg) { if(!--aux_seg1){aux_seg++,aux_seg1++;flag_1rec=0;}} //<acrescentar
flag_rok=0; // <acrescentar
recebe(); // <acrescentar
if((flag_rok)&&(!flag_1rec)){
conf=buffer[1]+buffer[2]+buffer[3];
if(conf==buffer[0]){flag_1rec=1;aux_seg1=tempo_seg;flag_rok=0;}// // <acrescentar
}

if((flag_rok)&&(flag_1rec)){
conf=buffer[1]+buffer[2]+buffer[3];
if(conf==buffer[0]){flag_acionar=1; }
}

if(flag_acionar){ // <acrescentar
flag_1rec=0; // <acrescentar
flag_acionar=0; // <acrescentar
//if(flag_rok){ //<comentar
// conf=buffer[1]+buffer[2]+buffer[3]; //<comentar
// if(conf==buffer[0]){ //<comentar
//
if((buffer[3]==make8(serial_number,1))&&(buffer[2]==make8(serial_number,0))){
aux=buffer[1];

//if(!bit_test(aux,3)){
if ((aux & (1<<3)) == 0){
cont_c1=t_inv;
#ifdef c1_ret
if(!fc1){
c1=!c1; fc1=1;
}
#else
#ifdef c1_inv
c1=0;
#else
c1=1;
#endif
#endif
}
//if(!bit_test(aux,1)){
if ((aux & (1<<1)) == 0){
cont_c2=t_inv;
#ifdef c2_ret
if(!fc2){
c2=!c2; fc2=1;
}
#else
#ifdef c2_inv
c2=0;
#else
c2=1;
#endif
#endif
}

// if(!bit_test(aux,2)){
if ((aux & (1<<2)) == 0){
cont_c3=t_inv;
#ifdef c3_ret
if(!fc3){
c3=!c3; fc3=1;
}
#else
#ifdef c3_inv
c3=0;
#else
c3=1;
#endif
#endif
}

//if(!bit_test(aux,4)){
if ((aux & (1<<4)) == 0){
cont_c4=t_inv;
#ifdef c4_ret
if(!fc4){
c4=!c4; fc4=1;
}
#else
#ifdef c4_inv
c4=0;
#else
c4=1;
#endif
#endif
}

//if(!bit_test(aux,5)){
if ((aux & (1<<5)) == 0){
cont_c5=t_inv;
#ifdef c5_ret
if(!fc5){
c5=!c5; fc5=1;
}
#else
#ifdef c5_inv
c5=0;
#else
c5=1;
#endif
#endif
}

}
// }
flag_rok=0;
}//if(flag_rok)
}//while
}//main

 


   
ReplyQuote
(@fatevian)
Active Member
Joined: 5 anos ago
Posts: 4
Topic starter  

Testado apenas no Proteus Isis


   
ReplyQuote
(@clarios)
Reputable Member Admin
Joined: 12 anos ago
Posts: 357
 

Fatevian!

Parabéns por sua iniciativa de compartilhar o código em Mikroc e por sua persistência!


   
ReplyQuote
(@fatevian)
Active Member
Joined: 5 anos ago
Posts: 4
Topic starter  

Obrigado.


   
ReplyQuote
Back To Top