Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

HELP C to Osho conversion

Status
Not open for further replies.

mastero

Member
Hello all,

This is an RF project need to convert the C-code to Osho .....

PIC16f628A

anyone please, thanx in advance.


RX-CODE:
C:
#define _LEGACY_HEADERS
//#include <htc.h>
#include <xc.h>


// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = ON       // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

/********** VERIABLE **********/
/* Data bit */
#define Bit0    0x01                //Data bit 0
#define Bit1    0x02                //Data bit 1
#define Bit2    0x04                //Data bit 2
#define Bit3    0x08                //Data bit 3
#define Bit4    0x10                //Data bit 4
#define Bit5    0x20                //Data bit 5
#define Bit6    0x40                //Data bit 6
#define Bit7    0x80                //Data bit 7


#define TIME    10000                //10 Sec
#define CR        0x0d  
#define LF        0x0a

#define ON        0
#define OFF        1

/* PORTA REGISTER */

/* PORTB REGISTER */
#define SOUNDEN RB4        //Sound Enable
#define LEDSTT RB5        //LED STATUS


static bit Alarmflag;    //
static bit Recflag;

unsigned char Recbuff[12];    // RX Buffer
unsigned char bank1 *Recptr;//Receive Pointer

unsigned char Delay3;
unsigned char Delay4;        //Learning Remote

unsigned int Delay1;
unsigned int Delay2;

union dat_long
{
   unsigned char byte[4];
   unsigned long word;
};
union dat_long Dat_long;

/********** initialize **********/
void init (void)
{  
    CMCON    =    0b00000111;            //PORTA DIGITAL I/O
    PORTA    =     0xff;                //SETUP PORT
    PORTB    =    0b11000111;
    TRISA    =    0b11111111;
    TRISB    =    0b11000111;
    OPTION    &=    0b01111111;           //PORTB INTERNAL FULL UP ON

    Recflag     = 0;
    Alarmflag     = 0;
    Delay1        = 0;
    Delay2        = 0;
    Delay3        = 0;
    Delay4        = 0;
    SOUNDEN     = OFF;  
}

void init_timer2()    //8 Bits
{
    //4MHz/4MC = 1 Meg
    //25*10*4 = 1000
    PR2        = 24;
    T2CON    = 0b01001101;            //PRE = 1:10,POST = 1:4
    PEIE    = 1;                    //
    GIE     = 1;
    TMR2IE  = 1;                    //Enable Timer2 Overflow Interrupt
}

/********** Function usart **********/
void usart (void){
    unsigned char a;
    CLRWDT();
    RCSTA = 0b10010000;
    TXSTA = 0b00000000;
    SPBRG = 103;           //SPBRG = 207 -->300 BPS
                            //SPBRG = 103 -->600 BPS
                            //SPBRG = 51  -->1200 BPS
                            //SPBRG = 25  -->2400 BPS
    RCIE  = 1;
    a         = RCREG;
    a         = RCREG;
    a         = RCREG;
    ei();
}

void clrbuf()
{
    unsigned char i;
    for(i=0;i<=9;i++)
    Recbuff [i] = 0;
}

static void interrupt isr(void)
{
    unsigned char temp;
//----------------USART Interrupt------------------
    if ((RCIE) && (RCIF))
    {  
        if (OERR){
            CREN = 0;
            CREN = 1;
        }
        if (RCREG == ':')
        {  
          
            temp    = RCREG;
            Recptr  = &Recbuff[0];    //Receiver Pointer @Recbuf[0]
            Recflag = 1;
        }
        else
        if (RCREG == CR)
        {          
            RCIE  = 0;
            if((Recbuff[0]=='C')&&(Recbuff[1]=='M')&&(Recbuff[2]=='D')&&(Recbuff[3]=='0')&&(Recbuff[4]=='1'))
            {
                clrbuf();
                Alarmflag = 1;
                SOUNDEN = ON;  
                Delay2 = TIME;
            }  
            RCIE  = 1;
        }
        else
        if (Recflag)
        {
            *Recptr = RCREG;            //      
            if (Recptr < &Recbuff[10])    //  
                Recptr++;
        }
    }
    else
//----------------Timer1 Interrupt------------------
//Every 0.001 Sec
    if((TMR2IE)&&(TMR2IF))                    //Over Flow
    {
        TMR2IF = 0;                //Clear Over flow
        if(Delay2>0)
            Delay2--;

        if((Delay2==0)&&(Alarmflag))   
            SOUNDEN = OFF;          
          
        Delay1++;
        if(Delay1>=1000)
            Delay1 = 0;

        if(Delay1<=50)
            LEDSTT = 1;
        else
            LEDSTT = 0;
    }
}



/********** Function DELAY time **********/
void dmsec (unsigned int Count)
{    unsigned char i;
    while (Count)
    {
        for (i=0;i<=128;i++);
        Count--;
        //    CLRWDT();
    }
}

//***************************************************//
//                       MAIN                       //
//***************************************************//
void main (void)
{  
    init();
    usart();
    init_timer2();
    while(1)
    {
/*
        if(Rfflag)
        {

            Rfflag = 0;
        }
*/
    }          
}

TX-CODE:

C:
#define _LEGACY_HEADERS
#include <xc.h>

// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = ON       // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

/********** VERIABLE **********/
/* Data bit */
#define Bit0    0x01                //Data bit 0
#define Bit1    0x02                //Data bit 1
#define Bit2    0x04                //Data bit 2
#define Bit3    0x08                //Data bit 3
#define Bit4    0x10                //Data bit 4
#define Bit5    0x20                //Data bit 5
#define Bit6    0x40                //Data bit 6
#define Bit7    0x80                //Data bit 7

#define Freebyte    0x00          
#define Startb        ':'              
#define CR           0x0d
#define LF            0x0a
#define ID            678                //MAX 99999999

/* PORTA REGISTER */
#define TX_VCC  RA2               

/* PORTB REGISTER */
#define SW_OFF    RB7 
#define SW_ON    RB6 
#define LED     RB4

static bit Onflag;
static bit Offflag;
unsigned char Idbuff[8];
/********** initialize **********/
void init (void)
{  
    CMCON        =    0b00000111;            //PORTA DIGITAL I/O
    PORTA        =     0x11111011;            //SETUP PORT
    PORTB        =    0b11001111;
    TX_VCC     =   0;
    TRISA        =    0b00111011;
    TRISB        =    0b11001111;
    OPTION        &=    0b01111111;           //PORTB INTERNAL FULL UP ON
    RBIE        = 1;
    Onflag     = 0;
    Offflag     = 0;
    ei();
}


/********** service routine interrupt **********/
static void interrupt isr(void)
{
    if((RBIE)&&(RBIF))
    {  
        if(!SW_ON)
        Onflag = 1;

        if(!SW_OFF)
        Offflag = 1;

        RBIF = 0;
    }
}
/********** Function usart **********/
void usart (void){
    RCSTA = 0b10010000;        //SPEN=1,CREN=1
                            //Asynchronous serial port enable
                            //continuous receive
    TXSTA = 0b00100000;        //Low speed TXEN = 1, SYNC = 0
                            //Transmit enable
    SPBRG = 103;               //SPBRG = 207 -->300 BPS
                            //SPBRG = 103 -->600 BPS
                            //SPBRG = 50  -->1200 BPS
                            //SPBRG = 25  -->2400 BPS
    TXIF = 1;
}


/********** Function sbyte **********/
void sbyte (unsigned char dat)
{
    while (!TXIF);            //WAIT FOR SEND OK
    TXREG = dat;
}

void sempty ()
{
    while (!TRMT);
}

/********** Function DELAY time **********/
void dmsec (unsigned int Count)
{    unsigned char i;
    while (Count)
    {
        for (i=0;i<=128;i++);
        Count--;
        //    CLRWDT();
    }
}
void send_on()                //Send On
{

    TX_VCC = 1;
    LED = 1;

    sbyte(Freebyte);    sbyte(Freebyte);
    sbyte(Startb);
    sbyte('C');            //
    sbyte('M');            //
    sbyte('D');            //
    sbyte('0');            //
    sbyte('1');            //
    sbyte(CR);
    sbyte(LF);
    sempty();  

    TX_VCC = 0;
    dmsec(50);
    LED = 0;
    Onflag = 0;  

}
void send_off()                //Send On
{

    TX_VCC = 1;
    LED = 1;

    sbyte(Freebyte);    sbyte(Freebyte);
    sbyte(Startb);
    sbyte('C');            //
    sbyte('M');            //
    sbyte('D');            //
    sbyte('0');            //
    sbyte('2');            //
    sbyte(CR);
    sbyte(LF);
    sempty();  

    TX_VCC = 0;
    dmsec(50);
    LED = 0;
    Onflag = 0;  

}
/********** main **********/
void main (void)
{  
    unsigned char ii;
    init();
    usart();
    //init_timer1();
    while(1)
    {
        send_on();
        dmsec(100);
    }          
}
 
Last edited:
This code is incomplete.... There are errors and it doesn't compile in C...

I have tried to convert but either it isn't going to work, or I won't be able to test it!! There is a miss-match of if - else statements.... Without the protocol, I'll be guessing!!
 
Hello Ian,

Try now i have made the required changes as this was an old code i found on the net needed some tweaks eg #define _LEGACY_HEADERS etc

it compiles now without error tried on MPLAB X IDE with MPLAB XC8 compiler.

basically my issue is i am very good with oshonsoft but C :(

this software in C is for 16f628A i have tried it physically and works perfectly.

i want to use this software in 12f675 which does not have USART so have to use Serin and Serout but not getting the stability as this software has

so want to understand what is different here.

any help shall be highly appreciated.

Thanx in advance

cheers
Mastero
 
The software USART will not be accurate in the 12F675 unless you ensure the chip has been calibrated (Pickit2 or 3 can do it), then move the calibration value into the OSCCAL register:
(Use ASM directives)
bsf STATUS, RP0 ;Bank 1
call 3FFh ;Get the cal value
movwf OSCCAL ;Calibrate
bcf STATUS, RP0 ;Bank 0

See the 675 datasheet, section 9.2.5.1
I had such an issue with software UART speeds in a 12F675 once, forgetting to calibrate the 4Mhz clock properly via OSCCAL. It was running about 10% fast. The 628A does not have an OSCCAL correction, it is calibrated at factory.

Also, the 675 does not have a timer2. You may be able to use timer0, but check what period the code is trying to set.

I would suggest writing/converting one section of Basic code at a time, and run it through the simulator. Then, combine the code segments together
 
Hello sagor,

The issue is converting the C to osho like i said earlier i don't know C

can you convert it ?

thanx
 
Well, I don't know C, so we are both at the same dis-advantage. Short snippets of code I could probably figure out, but code with various timers, interrupts, etc. written in C starts to get complicated. I'll have to pass, sorry. Maybe someone else can take this on, someone that knows C well...
 
Sagor need help with this c code want to change the ports for led etc.

I tried changing the TRIS and PORT setting but the whole program goes for a toss with that.

the simple change i need is

in TX
#define TX_VCC RA2 change to RB3
#define LED RB4 change to RB0

in RX
#define SOUNDEN RB4 change to RB3
#define LEDSTT RB5 change to RA6

any help shall be great.

regards
Mastero
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top