Author Topic: problems with C code on pic 16f886 (digital psu control with encoder)  (Read 1943 times)

0 Members and 1 Guest are viewing this topic.

Offline little_carlosTopic starter

  • Regular Contributor
  • *
  • Posts: 133
Hello! i made a psu with digital control by pic 16f886, i made the code and it worked great, until i wanted to put a rotary encoder as the controller.
i first did it with buttons and it worked great!, but when i implemented the encoder code, it started to beheave weird. the pwm doesnt respond as how it sould, like slow, and the lcd crashes and starts to show wierd characters. when i test the encoder code alone without the lcd and adc convertion, it works fine, the pwm increases and decreases as how it should, but when i put the lcd code back everything crashes as soon as i move the encoder, what is going on here? im usking mikro C and the frecuency is 16mhz
here is the code:
Code: [Select]
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;


float duty;
float v;
char volt [12];
char amp [12];
float a;


void main() {
TRISA = 1;
PORTA = 0;
TRISB = 0;
TRISC = 0;
PORTC = 0;
C1ON_bit = 0;
C2ON_bit = 0;
ANSEL = 0b00000011;
ANSELH = 0;
PWM1_init(2000);
Pwm1_start();
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
duty = 0;

while(1){

if(ra2_bit == 1){
if(ra3_bit == 1){
duty = duty+10.66;
}
}
if(ra3_bit == 1){
if(ra2_bit == 1){
duty = duty-10.66;
}
}

v = adc_read(0)*0.02725;
a = adc_read(1)*7.35;
floatToStr(v,volt);
floatToStr(a,amp);
lcd_out(1,1,"V:");
lcd_out(1,4,volt);
lcd_out(2,1,"mA:");
lcd_out(2,4,amp);
pwm1_set_duty(duty);


}
}

thanks for your time
 

Offline snarkysparky

  • Frequent Contributor
  • **
  • Posts: 414
  • Country: us
Re: problems with C code on pic 16f886 (digital psu control with encoder)
« Reply #1 on: January 21, 2017, 03:31:36 pm »
You might need to use one shots for ra2 and ra3.   I think you want to change the values only on the rising edges of ra2 and ra3.

unsigned int ra2_ctl,  ra3_ctl, ra2_os, ra3,os;

ra2_os = 0;               //   ra2 one shot
if((ra2_bit == 1)
{
     if(ra2_ctl == 0)
     {
          ra2_os = 1;
          ra2_ctl = 1;

     }
}
else
{
      ra2_ctl = 0;


}

same for ra3
           
use ra2_os  and ra3_os      in your code for changing duty





 

Offline little_carlosTopic starter

  • Regular Contributor
  • *
  • Posts: 133
Re: problems with C code on pic 16f886 (digital psu control with encoder)
« Reply #2 on: January 21, 2017, 04:35:43 pm »
You might need to use one shots for ra2 and ra3.   I think you want to change the values only on the rising edges of ra2 and ra3.

unsigned int ra2_ctl,  ra3_ctl, ra2_os, ra3,os;

ra2_os = 0;               //   ra2 one shot
if((ra2_bit == 1)
{
     if(ra2_ctl == 0)
     {
          ra2_os = 1;
          ra2_ctl = 1;

     }
}
else
{
      ra2_ctl = 0;


}

same for ra3
           
use ra2_os  and ra3_os      in your code for changing duty

i tried it but the lcd keeps showing weird things,it mixes the values on all the screen and ends up on a weird thing, do you have any idea of what is happening?
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 11885
  • Country: us
Re: problems with C code on pic 16f886 (digital psu control with encoder)
« Reply #3 on: January 21, 2017, 06:48:48 pm »
when i implemented the encoder code, it started to beheave weird. the pwm doesnt respond as how it sould, like slow, and the lcd crashes and starts to show wierd characters. when i test the encoder code alone without the lcd and adc convertion, it works fine, the pwm increases and decreases as how it should, but when i put the lcd code back everything crashes as soon as i move the encoder, what is going on here?

It sounds like you may have some kind of memory collision/overwriting problems happening. If each part of the code works OK by itself then the algorithms are probably correct. But if the program goes wrong when you put it all together it suggests one part of the program may be writing over the memory used by another part of the program.

You should try compiling with whatever diagnostic options and debug checks are available in that compiler to see if you can find any warnings. Are you sure the chip you are using has enough memory available for the size of the program and variables you are using?
 

Online rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: problems with C code on pic 16f886 (digital psu control with encoder)
« Reply #4 on: January 21, 2017, 07:37:02 pm »
I usually start by commenting out blocks of code until I get something working.  Then I reintroduce the blocks one by one until I find the point of failure.
 

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 7733
  • Country: ca
Re: problems with C code on pic 16f886 (digital psu control with encoder)
« Reply #5 on: January 22, 2017, 02:50:10 am »
You seem to have the TRIS for those 2 pins set to output.  You might be crashing the MCU if you are forcing data into those 2 pins just from the potential current spikes.
 

Offline snarkysparky

  • Frequent Contributor
  • **
  • Posts: 414
  • Country: us
Re: problems with C code on pic 16f886 (digital psu control with encoder)
« Reply #6 on: January 22, 2017, 03:02:17 am »
I was thinking that you are updating duty constantly even when ra2 and ra3 are true. 
the code is scanning rapidly and you only want to update when the inputs change.

I'll take another look at my one shot proposal when I sober up.

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf