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] Please help me to correct the code(I am using PIC16F628A)

Status
Not open for further replies.

srenjis

Member level 1
Joined
Jun 30, 2012
Messages
38
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,288
Activity points
1,566
I am totally new to C. I was trying for a PWM Controller with up down switch & 3 push buttons to on &off three led. the push button & led part is working ,but when i add pwm code it isnot working

please help me to correct the code the

Here is the C program written for MikroC PRO
 

Attachments

  • New Folder.zip
    36.1 KB · Views: 107

[/CODE]one LED is driven by the PWM output from the pic through a current limit resistor . The duty cycle of the output PWM signal is increased or decreased with the two tact switch inputs.
And there is another 4 outputs controlled by individual switches for on & off

the 4 out puts are working fine with this code


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
sbit LED3 at Rb4_bit;
  sbit LED2 at Rb2_bit;
 sbit LED at Rb0_bit;
 sbit LED1 at Rb1_bit;
 //sbit Switch at RC1_bit;
 // #define Switch_Pin 1
  //#define Switch_Port PORTC
  //#define Debounce_Time 20
unsigned short oldstate;
unsigned short last_duty, current_duty;
void main()
{
  CMCON |= 7;                                     // Main;
 TRISB = 0;                           // Set PORTB direction to be output;
 PORTB = 0;                        // Turn OFF LEDs on PORTA;
   trisb3_bit = 0;
 TRISA6_bit = 1;                      // Set PORT RB1 input for button;
 oldstate = 0;                        // Define value of oldstate;
  current_duty = 0;          // Initial value of variable current_duty;
 last_duty = 0;             // Reset variable last_duty;
 pwm1_init(5000);           // PWM module initialization (5KHz);
 pwm1_start();              // Start PWM1 module;
 pwm1_set_duty(last_duty);
 
 
 
 
 
 
 
 
 
 
 
 
do {
 
 
            // 200mS delay
 
 
 
 
 
 
 
     if (Button(&PORTA,0,1,1)) oldstate = 1;
    Delay_ms(100);                                              //delay for prevent crash
    if (oldstate &&Button(&PORTA,0,1,0)) {
                                        led = ~led;       //invert state of PORTB
                                          oldstate = 0;
                                          }
 
 
    if (Button(&PORTA,1,1,1)) oldstate = 1;
    Delay_ms(100);                                              //delay for prevent crash
    if (oldstate &&Button(&PORTA,1,1,0)) {
                                        led1 = ~led1;       //invert state of PORTB
                                          oldstate = 0;
                                          }
 
    if (Button(&PORTA,2,1,1)) oldstate = 1;
    Delay_ms(100);                                              //delay for prevent crash
    if (oldstate &&Button(&PORTA,2,1,0)) {
                                        led2 = ~led2;       //invert state of PORTB
                                          oldstate = 0;
                                          }
       if (Button(&PORTA,3,1,1)) oldstate = 1;
    Delay_ms(100);                                              //delay for prevent crash
    if (oldstate &&Button(&PORTA,3,1,0)) {
                                        led3 = ~led3;       //invert state of PORTB
                                          oldstate = 0;
                                          }
 
 
    } while(1);                                                 //Infinite loop






but when i add the pwm code it is nor working




Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
while(last_duty>0<255){
                            if (Button(&PORTA, 6,1,0))
                            last_duty = last_duty +2;       // If the button connected to
                                                            // RA0 is pressed
                            Pwm1_Set_Duty(last_duty);
                            delay_ms(100);
 
                            if (Button(&PORTA, 7,1,0))
                            last_duty = last_duty -2;       // If the pressed button is
                                                            // connected to RA1
                            Pwm1_Set_Duty(last_duty);
                            delay_ms(100);
                            }
     delay_ms(200);

 
Last edited:

Why RA3 is not used?

Maybe there is a bug in Proteus 16F628A model because LED flickers even if PWM duty is 255. Test in hardware.
 

Attachments

  • PWM.rar
    33.9 KB · Views: 110
Thank you four helping me


the code was not working in hardware. only the pwm out glows with full brightness .the push buttons are not working.
Why RA3 is not used? is it related to pwm?
 

Post your Proteus circuit in PDF format. My Proteus is crashing if I open your .DSN file.
 

View attachment yyy.pdfThe circuit is attached . In this ckt the switches are working with the hex code in post #1,in real hardware .pwm is not working .
but when i tried with the code& ckt in post#5 only the pwm led is high and the switches are not working when tested in hardwire
 
Last edited:

I tested my code in post #5 and there is no problem. It works fine for me. I have made a small change in my code. I have increased PWM duty stepping from 1 to 15. PWM is working fine and also the buttons. Problem is in your hardware. See if you are using resistors with value greater than 330E for LEDs. I have attached new project and also Proteus 8.1 SP1 file. I have also included Proteus simulation video. See video inside attached file. Everything is working.
 

Attachments

  • PWM.rar
    381.2 KB · Views: 112
Last edited:
It works fine in proteus only. .


in hardware i use 470e resistors for led ,but not working,only the pwm led is on ,switches not working
 

What color LED are you using? you need 10 mA current for LEDs. Find (5V - LED V) / 470
 

In my hardware i use red led (normal) with a 470e resistor .
 

pic and the buttons are in working condition. When i use the code in post #1 all are working except pwm



can you please explain these lines

while((SW_PORT & 0xF7) != 0xF7);


#define SW_TEST_VAL 0xD7
 

SW_TEST_VAL is a mask value used for testing which button is pressed. It works only for my circuit. If your circuit is different then the code will not work.

PORTA is used for Switches. 0xF7 = 0b11110111. According to your circuit buttons pins will be always high and goes low if button is pressed.

If RA0 is pressed then value will be 0b11110110.

SW_PORT value & mask value is
0b11110110 & 0b11110111 = 0b11110110 which is not equal to 0b11110111 which means button is pressed but not yet released. When button is released then SW_PORT & 0xF7 becomes 0xF7 and loop breaks.

Try replacing


Code C - [expand]
1
while((SW_PORT & 0xF7) != 0xF7);



with


Code C - [expand]
1
while(SW_PORT != 0xF7);

 
changed as while(SW_PORT != 0xF7); but not working

the switch read values in your code are d6,d5,d3,c7,97 ,but in this case ra5 (reset pin) is treated as '0' at the same time it is connected with vcc via 10k

Code:
 if((SW_PORT & SW_TEST_VAL) == 0xD6) {
                   Delay_ms(80);
                   testButtonRelease();
                   LED1 = ~LED1;
            }
            else if((SW_PORT & SW_TEST_VAL) == 0xD5) {
                   Delay_ms(80);
                   testButtonRelease();
                   LED2 = ~LED2;
            }
            else if((SW_PORT & SW_TEST_VAL) == 0xD3) {
                   Delay_ms(80);
                   testButtonRelease();
                   LED3 = ~LED3;
            }
            else if((SW_PORT & SW_TEST_VAL) == 0xC7) {
                   Delay_ms(80);
                   testButtonRelease();
                   LED4 = ~LED4;
            }
            else if((SW_PORT & SW_TEST_VAL) == 0x97) {
                   Delay_ms(80);
                   testButtonRelease();
                   if((gPwmDuty >= 0) && (gPwmDuty <= 254)) {
                            gPwmDuty++;
                            PWM1_Set_Duty(gPwmDuty);
                   }
            }
            else if((SW_PORT & SW_TEST_VAL) == 0x57) {
                   Delay_ms(80);
                   testButtonRelease();
                   if((gPwmDuty <= 255) && (gPwmDuty > 0)) {
                            gPwmDuty--;
                            PWM1_Set_Duty(gPwmDuty);
                   }
 

MCLR pin is RA5 and it remains high all the time.
 

how to store the last values of the led's to internal ram , so that if the power fails the led,s shows the last condition
 

You have to save both LED PORT value and PWM duty ratio value. When LED Port value should be ANDed with bit 0 for all unused pins of LED port and pwm pin.
 

can you please give one example to do this
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top