problems with C code on pic 16f886 (digital psu control with encoder) - Page 1
Forum section: electronics / beginners

author: little_carlos
author_rank: Regular Contributor
timestamp: January 21, 2017, 02:56:56 pm
content: 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 16mhzhere is the code:Code: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 ;char amp ;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

author: snarkysparky
author_rank: Frequent Contributor
timestamp: January 21, 2017, 03:31:36 pm
content: 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 shotif((ra2_bit == 1){if(ra2_ctl == 0){ra2_os = 1;ra2_ctl = 1;}}else{ra2_ctl = 0;}same for ra3use ra2_os and ra3_os in your code for changing duty

author: little_carlos
author_rank: Regular Contributor
timestamp: January 21, 2017, 04:35:43 pm
content: Quote from: snarkysparky on January 21, 2017, 03:31:36 pmi 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?

author: IanB
author_rank: Super Contributor
timestamp: January 21, 2017, 06:48:48 pm
content: Quote from: little_carlos on January 21, 2017, 02:56:56 pmIt 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?

author: rstofer
author_rank: Super Contributor
timestamp: January 21, 2017, 07:37:02 pm
content: 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.

author: BrianHG
author_rank: Super Contributor
timestamp: January 22, 2017, 02:50:10 am
content: 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.

author: snarkysparky
author_rank: Frequent Contributor
timestamp: January 22, 2017, 03:02:17 am
content: 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.