Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Const shows error in sine table of a sine wave inverter code with PIC16F73

Status
Not open for further replies.

Mithun_K_Das

Advanced Member level 3
Joined
Apr 24, 2010
Messages
896
Helped
24
Reputation
48
Reaction score
25
Trophy points
1,318
Location
Dhaka, Bangladesh, Bangladesh
Activity points
8,227
Error in wave inverter code with PIC16F73

I was trying to develop a simple sine wave inverter using PIC16F73 MCU.
Compiler: mikroC pro for PIC v7.6.0
Simulator: Proteus 8.9

Code:
Code:
//sine wave generation using PIC16F73 test
//Crystal: 20MHz.


unsigned int sin_table[32]={0, 25, 49, 73, 96, 118, 139, 159, 177, 193, 208, 220, 231, 239, 245, 249, 250, 249, 245, 239, 231, 220, 208, 193, 177, 159, 139, 118, 96, 73, 49, 25};
int duty=0;
bit alt;
void Interrupt() iv 0x0004 ics ICS_AUTO
{
   if (TMR2IF_bit == 1)
   {
      duty++;

      if(duty>32)
      {
        duty=0;
        alt=~alt;
      }
      
      if(alt)
      {
         CCPR1L = sin_table[duty];
         CCPR2L=0;
         RC0_bit=1;
         RC3_bit=0;
      }
      else
      {
         CCPR2L = sin_table[duty];
         CCPR1L=0;
         RC0_bit=0;
         RC3_bit=1;
      }
      TMR2IF_bit = 0;
   }
}

void main() 
{
   TRISC = 0x00;
   PR2 = 249;
   CCP1CON = 0x4C;
   CCP2CON = 0x4C;
   TMR2IF_bit = 0;
   T2CON = 0x14;
   TMR2IF_bit = 0;
   TRISC = 0;
   TMR2IE_bit = 1;
   GIE_bit = 1;
   PEIE_bit = 1;

   while(1)
   {

   
   }
}


//


I was simulating in proteus. Still not developed in hardware.
This code is working fine in simulation:
s21.PNG
Problem is when I'm declearing the sine table as Const proteus can't simulate it, shows:

s22.PNG

Can anyone tell me whats wrong here?
 
Last edited:

I wonder if there's an actual problem of PIC16 performing flash program read during interrupts or it's just a simulator bug.

For the present code, there's no advantage of using interrupts while executing an empty loop. Instead you can poll timer flag in the loop. Being even faster than performing interrupt context switching.
 
That means it should work in real hardware, right? The while loop is blank as I just started coding. In the first step I found that unexpected error. Recently I've completed one with dsPIC based pure sine wave which was excellent. Just started a new small one with PIC16F73. Hope anyone who are looking for similar code will be benefited from this post.
 

The small PIC processors are not good for C compilers. As an example, they can't read from program memory, so a constant lookup table is made from an "array" of return instructions.
The indexing is done by jumping to the right return instruction.
You should look at the assembler code generated by the compiler.

If you really want to learn the limitations of the small PIC processors, you can use a non-ANSI compiler from https://www.bknd.com/
Those compilers simply don't support constructs that don't map well to the processor's instruction set.
The compiler will give an error when you do something "stupid".
 

I would try declaring sin_table as unsigned char instead of int once done as above waste memory filling with zero (const values are smaller than 255). As far as I remember, the PIC16F family has limitations reagarding to the size of arrays, which I would not expect to see in the above case.
 

I would try declaring sin_table as unsigned char instead of int once done as above waste memory filling with zero (const values are smaller than 255). As far as I remember, the PIC16F family has limitations reagarding to the size of arrays, which I would not expect to see in the above case.

OK, then what should be used for declaring sine table maybe 4-5 different one for feedback control? Using same type of array is consuming lots of RAM. What is the solution then?

- - - Updated - - -

I tried with multiplying with sine table. It works. Feedback possible.

IMG_20200205_121149.jpg

IMG_20200205_121202.jpg

Code:
 //sine wave generation using PIC16F73 test
//Crystal: 20MHz.



unsigned char sin_table[32]={0, 25, 49, 73, 96, 118, 139, 159, 177, 193, 208, 220, 231, 239, 245, 249, 250, 249, 245, 239, 231, 220, 208, 193, 177, 159, 139, 118, 96, 73, 49, 25};
int duty=0;
bit alt;
void Interrupt() iv 0x0004 ics ICS_AUTO
{
   if (TMR2IF_bit == 1)
   {
      duty++;

      if(duty>=32)
      {
        duty=0;
        alt=~alt;
      }

      if(alt)
      {
         CCPR1L = sin_table[duty]*0.5;
         CCPR2L=0;
         RC0_bit=1;
         RC3_bit=0;
      }
      else
      {
         CCPR2L = sin_table[duty]*0.5;
         CCPR1L=0;
         RC0_bit=0;
         RC3_bit=1;
      }
      TMR2IF_bit = 0;
   }
}

void main() 
{
   TRISC = 0x00;
   PR2 = 251;
   CCP1CON = 0x4C;
   CCP2CON = 0x4C;
   TMR2IF_bit = 0;
   T2CON = 0x2C;
   TMR2IF_bit = 0;
   TRISC = 0;
   TMR2IE_bit = 1;
   GIE_bit = 1;
   PEIE_bit = 1;

   while(1)
   {
      //will add later
   
   }
}


//
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top