Why won't this create PWM?

Thread Starter

CumminsSuburban

Joined Sep 18, 2022
12
PWM converter-1.jpgGood afternoon,

This is my first post here. I am just a hobbyist, but starting playing with PICs over a year ago. My current project needs to take an analog signal and convert it to PWM.
So this is for an automotive project. It's supposed to take a 0-5V variable voltage from the throttle input and the converter should send a variable duty cycle (0-100) to the transmission control module so it knows when to shift the transmission.
It works as its supposed to on the bench using a POT connected to the converter.
When I hook it up to the vehicle is where i have issues. I have verified correct voltage from the TPS input, but it will only create a 10 percent duty cycle no matter what voltage the PIC sees from the input. I'm using a Labrador to verify the PWM (oscilloscope) on my laptop. The Labrador is also grounded to the vehicle. I'm literally ripping my hair out and cannot figure this out, its been months! Makes no sense.

Hopefully someone here can finally solve this. Thanks!
 

Thread Starter

CumminsSuburban

Joined Sep 18, 2022
12
Thank you for the reply. Is the noise something that can be filtered by a low/high pass filter with a capacitor and resistor? Sorry if this sounds ignorant. I am trying to learn as I go. I will try to measure the frequency of the input. The PWM output should be 133HZ, but I could only get it to output 70HZ.
 

Jerry-Hat-Trick

Joined Aug 31, 2022
822
Thankfully I googled Labrador oscilloscope, I was about to call the RSPCA :D
Just wondering, are you using the body/chassis for ground? Maybe it's just the way you've drawn it, but I do thinnk the ground and 5V rail should be direct wire links between the TPS and processor, and the the wiper line, maybe together is a screened cable? I also wonder why the need for the diode
 

Alec_t

Joined Sep 17, 2013
15,117
I can't see any need for the diode either, unless it's to block load-dump reverse voltage spikes on the +ve supply rail. You need good supply rail decoupling and screened wiring. The wiper of the pot could also have a capacitor to ground to suppress noise.
 

Thread Starter

CumminsSuburban

Joined Sep 18, 2022
12
Sorry for the confusion. The TCM will output a voltage (5v & 12v) from the input ports when its not receiving a PWM value (PCM demands it). I have tested the circuit with a POT instead of the TPS to verify it all works. The 5V tps voltage is the only variable causing it not to work.

Without the diode, the TCM will send power back into the TPS and into the engine ECU, (thinks the engine is at wide open throttle) causing the engine to rev up until it gets a fault and disables the throttle control.

I have made some "progress". I made a low pass filter with a diode/resistor. It finally reads 0% at no throttle, but still only reaches a 10% value at wide open throttle (5V). Ive been diligently working on it to figure it out.
 

Thread Starter

CumminsSuburban

Joined Sep 18, 2022
12
I believe I have finally figured out my issue. The TPS is outputting such low current (less than 12ma) that the PIC isnt able to see the voltage, but my multimeter obviously can. The POT on the bench has over 30ma at around half its range and 70ma maxed out. So back to the drawing board...again.
 

wraujr

Joined Jun 28, 2022
259
Please give more details on your TPS input to your PIC. One would assume this is an A/D input for measuring the TPS sensor voltage and it would be high impedance and draw very little current. It would also not output any voltage/current and you would not need the diode.
 

wraujr

Joined Jun 28, 2022
259
First, a typical TPS uses a potentiometer and has three wires/connections:
(1) Ground
(2) 5V Reference (from TCM), this is low current and should NOT be used to power anything other than TPS
(3) Voltage Output, 0-5V depending on throttle position.

Your PIC should be using a high impedance A/D input to sample the Voltage Output of the TPS and require very little current.
The battery in your schematic must not be the car battery as you have labeled it 5V and cars are 12V.
You should be powering your PIC from the car's 12V system, you should not be using outputs from the TCM.
Any GND in you PIC needs to connect at car ground (same ground as used by TPS).
You need to create a full schematic with clear labeling.
 

Reloadron

Joined Jan 15, 2015
7,887
I believe I have finally figured out my issue. The TPS is outputting such low current (less than 12ma) that the PIC isnt able to see the voltage, but my multimeter obviously can. The POT on the bench has over 30ma at around half its range and 70ma maxed out. So back to the drawing board...again.
I seriously doubt that is the problem. This would go much better if as requested in Post #4.
Hello.

Can you please post the code you've uploaded into the PIC MCU?
All you need is an analog in to PWM out which should not be a problem. Less seeing your code and knowing your Analog In is good there is no way to make suggestions. I also see no reason for the diode in there.

Ron
 

mugatu09

Joined Oct 31, 2019
1
First thing I would try is disconnecting the transmission control module, using a power supply to control the TPS input, and seeing if the if the PWM out goes above 10%. Then add the transmission control module, and finally add the real TPS input.
 

wraujr

Joined Jun 28, 2022
259
I believe I have finally figured out my issue. The TPS is outputting such low current (less than 12ma) that the PIC isnt able to see the voltage, but my multimeter obviously can. The POT on the bench has over 30ma at around half its range and 70ma maxed out. So back to the drawing board...again.
Are you powering your PIC with the 5V going to the TPS? This is not a good idea and probably won't work. The 5V to TPS is a "reference voltage", not a power supply.
 

Thread Starter

CumminsSuburban

Joined Sep 18, 2022
12
I appreciate all of the input so far. I will post the code below for the PIC.

One note: The POT i am using has a range of 0-100 ohms, and that works perfectly for the setup.
The TPS in the vehicle has a range of 700-4500 ohms and i believe that's where my issue lies.

For the PIC setup; It recieves the signal on (AN1) and outputs two separate PWM signals out of CCP1 and CCP2. It is powered by it's own separate 5v supply and grounded at a junction that goes straight to the battery.

I may be way off track here, but would I be able to use a precision op-amp to mirror the voltage and overcome the high resistance while keeping the voltage the same? I'm no expert in small electronics, but I am a fast learner. Thanks for the help!

Code:
 // PIC18F4610 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1H
#pragma config OSC = INTIO67    // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-003FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (004000-007FFFh) not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (008000-00BFFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (00C000-00FFFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-003FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (004000-007FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (008000-00BFFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (00C000-00FFFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF. :)


//ISSUES: FREQUENCY STUCK AT 66HZ. NO MATTER WHAT I CHANGE. DUTY CYCLE SLIGHTLY OFF AT 50%
#include <xc.h>
#define _XTAL_FREQ 2000000
#define TMR2PRESCALE 16
long PWM_freq = 133; // 133HZ PWM OUTPUT

void PWM_Initialize() 
{
   PR2 = (_XTAL_FREQ / (PWM_freq * 4 * TMR2PRESCALE)) - 1; //Setting the PR2 formulae using Data sheet // Makes the PWM work in 133HZ 
   CCPR1L = 0b11101010;
   CCPR2L = 0b11101010;
   TRISCbits.RC1 = 0; 
   TRISCbits.RC2 = 0;
   T2CON = 0x03;   
   CCP1CON = 0x0C;
   CCP2CON = 0x0C;
   TMR2 = 0;
   T2CONbits.TMR2ON = 1; 
}

void PWM_Duty(unsigned int duty)
{
  if(duty < 1023)
  {
      CCPR1L = duty>>2;
      CCPR2L = duty>>2;
  }
}

void ADC_Initialize()
{    
   ADCON1bits.PCFG = 0;
   ADCON1bits.VCFG = 0;
   ADCON0 = 0;
   ADCON2bits.ACQT = 3;
   ADCON2bits.ADCS = 5;
   ADCON2bits.ADFM = 1;
   ADCON0bits.ADON = 1;
}

unsigned int ADC_Read(unsigned char ch)
{
   if(ch > 13)return 0;
   ADCON0 = 0;
   ADCON0 = (ch<<2);
   ADCON0bits.ADON = 1;
   ADCON0bits.GO_DONE = 1;
   while(ADCON0bits.GO_DONE == 1);
   ADCON0bits.ADON = 0;
   return ADRES;
}

void main()
{    
   int adc_value;  
   TRISC = 0;
   TRISA = 1;
   TRISD = 0; 
   ADC_Initialize();
   PWM_Initialize();

   do
   {
       adc_value = ADC_Read(1);
       
       PWM_Duty(adc_value);
       
       __delay_ms(50);
             
   }
   while(1); //Infinite Loop  
} [code]
 

Thread Starter

CumminsSuburban

Joined Sep 18, 2022
12
That was the plan.
I started with a 5K resistor and it wouldn't even start producing a PWM signal. I went down to a 330 ohm resistor and that brought a 100 percent duty cycle down to a 10 percent. Even a 100 ohm massively dropped the duty cycle. What would you recommend? Other than burning the truck of course, lol.
 
Top