1. 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.
    Dismiss Notice

PWM PIC16F676

Discussion in 'Homework Help' started by Fabianr18, Nov 27, 2017.

  1. Fabianr18

    Fabianr18 New Member

    Joined:
    Nov 27, 2017
    Messages:
    11
    Likes:
    0
    im new to using this language and im trying to figure out how to set up the micro controller to take 4 inputs and out put different pulse width modulation using C language if anyone can help thanks
     
  2. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,074
    Likes:
    524
    Location:
    Cleveland, OH, USA
    ONLINE
    You have 8 channels of ADC, but no CCP (PWM) module. So, you will need to write software PWM and control that with your 4 inputs. You have a 16-bit and 8-bit timers. I am assuming the inputs will be analog. If digital, then, will they be serial? Are you required to use that chip?

    Can you give the equations that describe the relationship of the 4 inputs to the PWM output? What resolution and frequency do you need for the PWM?

    EDIT: Are the "four inputs" to be consecutive bits of a port and considered a "nibble"? For example, that would give 16 choices for PWM duty cycle in its simplest form.
     
    Last edited: Nov 28, 2017
  3. Fabianr18

    Fabianr18 New Member

    Joined:
    Nov 27, 2017
    Messages:
    11
    Likes:
    0
    my instructions are posted on the figure below. figured that by using a while loop and for loops would create the PWMs that i need. any suggestions ( first time ever programming this type of chip)

    #include <xc.h>
    #pragma config FOSC = INTOSC
    #pragma config WDTE = OFF
    #pragma config MCLRE = ON
    #pragma config CP = OFF



    main()
    anselc=0b00000000;
    trisc=0b00001111; // pins rc(0123) are set to be inputs
    osccon=0b01101010; //4MHz
    long 1; //32 bits
    while(1) {
    if(RCRC1==RC2==RC3==0) //input 0000
    {for(i=0;i<17;i++){
    RC4=1;}
    for(i=0;i<17;i++){
    RC4=0;}
    }
     

    Attached Files:

    Last edited: Nov 28, 2017
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,074
    Likes:
    524
    Location:
    Cleveland, OH, USA
    ONLINE

    This looks like homework and should be moved to that section.

    That is a type of PWM, but your professor has defined the high periods, rather than duty cycle. There are 16 high periods from 200 us to 950 us. I would use one of the timers to set a recurring interrupt of at least 1 ms -- probably longer, e.g., >200 ms, is better. Then I would use the other timer to set a pulse width at each interrupt of 200 us to 950 us depending on a read of the port with 4 switches. I would pull the port pin to ground with a resistor of about 10K , then connect to Vcc with the switch. This is, a pin connected to a switch that is "on" will read high (1) and when off will read low (0).

    From that point, you need to design and post your schematic and code. Most people here do C, but I do not. I am sure they will chip in once you show your efforts.
     
    • Like Like x 1
  6. Fabianr18

    Fabianr18 New Member

    Joined:
    Nov 27, 2017
    Messages:
    11
    Likes:
    0
    I've been working on a PWM code on a PIC16f676 using XC8,mplab so far my code doesn't have any problems but doest upload to the microchip (first time ever using this chip) and the code seems logical it will generate a pulse width any suggestion why it wont upload maybe the connection of my pickit3

    #include <xc.h>
    #pragma config FOSC = INTOSC
    #pragma config WDTE = OFF
    #pragma config MCLRE = ON
    #pragma config CP = OFF



    main()
    ansel = 0x00;
    trisc=0b001111; // pins rc(0123) are set to be inputs
    osccon=0b01101010; //4MHz
    long i; //32 bits
    while(1) {
    if(RC0==RC1==RC2==RC3==0) //input 0000
    {for(i=0;i<17;i++){
    RC4=1;}
    for(i=0;i<17;i++){
    RC4=0;}
    }
     

    Attached Files:

  7. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,074
    Likes:
    524
    Location:
    Cleveland, OH, USA
    ONLINE
    Thanks for moving to homework.

    What I would do is setup a 50 us delay (with adjustment for the overhead of a call and return and etc. -- about 4 to 6 us @ 4 MHz). Then read the port nibble used for input and add "4". Thus, 0x00 = 200 us on and so forth. Keep the LED pin on until finished counting down from "port value + 4".

    John
     
  8. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,515
    Likes:
    507
    Location:
    Vermont (GMT-5:00)
    Both threads have been merged together in the Homework section.
     
  9. Fabianr18

    Fabianr18 New Member

    Joined:
    Nov 27, 2017
    Messages:
    11
    Likes:
    0
    thanks now my program is working but when i try to upload to the micro controller i get the following errors
    {
    Target device was not found (could not detect target voltage VDD). You must connect to a target device to use PICkit 3.
    Target has invalid calibration data (0x00). }

    ive connected VDD to power supply and VSS to ground and still get the error
     
  10. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,074
    Likes:
    524
    Location:
    Cleveland, OH, USA
    ONLINE
    VDD and VSS are, of course, required. Do you have the correct chip selected in the "Configure" dropdown? ( I use MPLab 8.92,so that may not be the exact wording.) Be sure the clock and data are connected too -- but that's a given.

    Oh, you need to tell the programmer to power the target. In MPLab 8.92, that is under Programmer> Settings> Power.

    PK3 has very low power capability (20 to 30 mA), so you may need to disconnect any peripherals that would draw more current than that.
     
    Last edited: Nov 28, 2017
    • Like Like x 1

Share This Page