Continue to Site

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.

  • 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.

Low Frequency AC->DC for 5V USB Buck

Status
Not open for further replies.
Here's the code I've come up for the 629. I haven't compiled it or anything, I just need to be ready to go when the kit arrives as I've a big tour through Tajakistan and hope to have it working by then.

I'm also considering moving the cap so that it's shared between the 629 and buck. Space is a consideration, plus the buck switches off at 4.5v, if the 629 doesn't shut it down first.
 

Attachments

  • usb.c
    1.1 KB · Views: 155
Scrap that code. Now using XC8. It compiles, and hopefully my logic is correct if anyone can have a quick look over!
 

Attachments

  • test.c
    1.5 KB · Views: 148
Sorry, I can't help you with a C program. I find C difficult to understand. I write code in assembler.

Les.
 
Here is a version written in assembler that should do what you want.
Code:
; Low frequency cut off detector

; Started 24/03/16
; Version 01
;
; 25/03/16
;  Version 02
;  Changing to use timer1 interrupting

;  Using internal oscillator 4.0 MHZ Fosc/4 = 1 MHZ (Period = 1 uS)






  list p=12f629
  errorlevel +302
  include "p12f629.inc"
  
;*******************************************************************************
; Configuration Bits
;*******************************************************************************

  __CONFIG _CPD_OFF & _CP_OFF & _BODEN_ON & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT  ;Internal oscillator

;  __CONFIG _CPD_OFF & _CP_OFF & _BODEN_ON & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _HS_OSC & _EXTRC_OSC_NOCLKOUT  ; Crystal

;*******************************************************************************
; Constants
;*******************************************************************************


RAM_START   equ  0x20
RAM_SIZE   equ  0x40


TMR1_Counts equ D'1000' 
Max_Retrig_Count equ D'50' ;Max number of milliseconds allowed between input pulses
Delay_On_Count equ D'20' ;Number of input pulses before Enable is set.



SDELAY_18_8uS equ D'16'
SDELAY_81_6uS equ D'68'

;I/O ports

; GPIO 0 Pin 7 Enable output
; GPIO 1  Pin 6 Input pulse
; GPIO 2  Pin 5  (Int)  (Use for testing as one shot output.)
; GPIO 3  Pin 4  (Can only be used as an input.)
; GPIO 4  Pin 3
; GPIO 5  Pin 2
;
  
;*******************************************************************************
; Pin Assignments
;*******************************************************************************



 #DEFINE Pulse_in GPIO, 1 

  
;*******************************************************************************
; File Register Variables
;*******************************************************************************
  cblock  RAM_START

;param1  ; parameter 1 for function calls  (Only seems to be used in delay routine)
;param2  ; parameter 2 for function calls  (Only seems to be used in delay routine)
;temp1  ; temporary scratch variable (Used in dealy routine

Count_L ;Holds value to be loaded into TMR1 Low byte
Count_H  ;Holds value to be loaded into TMR1 High byte
Retrig_Count  ;Counts number of milliseconds since it was reset by input pulse
 ;So for minimum input frequency of 20 HZ then if this count reaches 50 then set One_shot_out low
Delay_Count
 


Flags ;(Other bits not used)

 ; Bit 0 represents "One_shot_out"
 ; Bit 1
  endc
  
;*******************************************************************************
; Reset Vector
;*******************************************************************************
  org  0x000  ; processor reset vector
  goto  main  ; jump to main function

;*******************************************************************************
; Interrupt Vector
;*******************************************************************************
;  For 1 mS interrupts  we need to preset the timer register to (65536 - 1000) = 64536  = 0xFC18
  org  0x004  ; processor reset vector

 btfss  PIR1,TMR1IF  ;Is it a TMR1 interrupt ?
 GOTO  T1_02 ;No - return from interrupt
   bcf  PIR1,TMR1IF  
  bcf  STATUS,RP0  ; bank 0
  bcf  T1CON,TMR1ON  ; stop timer 1 (1 uS) (1 cycle)

;Load TMR1 for next delay.

 movlw  LOW TMR1_Counts  ;
  movwf  Count_L  ; 
  movlw  HIGH TMR1_Counts  ; 
  movwf  Count_H  
  
 COMF Count_L, W ; (1 uS) (1 cycle)
 MOVWF TMR1L  ; timer 1 (1 uS) (1 cycle)

 COMF Count_H, W
 MOVWF TMR1H  ; timer 1 (1 uS) (1 cycle)

 BSF  T1CON,TMR1ON  ; Start timer 1 (1 uS) (1 cycle)  (5 cycles from start of interrupt) 
 


 MOVLW Max_Retrig_Count
 SUBWF Retrig_Count, W
 BTFSC STATUS, Z 
 GOTO T1_01
 INCF  Retrig_Count
 GOTO T1_02
T1_01:
 BCF Flags,0
 BCF GPIO, 2 ;

T1_02:
 RETFIE ; (2 uS) (2 cycles)



;*******************************************************************************
; Function:  init
; Description: Intialize the PIC registers
; Parameters:  None
; Returns:  None
;*******************************************************************************
init:
  bsf  STATUS,RP0  ; bank 1
  
 MOVLW B'00000010' ;GPIO  1  as intput. All others outputs
  movwf  TRISIO  ; 1 - input, 0 - output


; movlw B'00000111' ; prescaler (128) internal clock and /256,  1.0 uS * 256 = 128  uS (Overflow every 51.2 * 256 = 13.1072 mS)
; movwf OPTION_REG ; pullups enabled

  bcf  STATUS,RP0  ; bank 0

  movlw  0x7  ; turn off the comparator module
  movwf  CMCON  ; 111 - comparator off
  
  bsf  T1CON,TMR1ON  ; enable timer1  All other bits 0  1:1 Prescaler ,Internal clock Fosc/4

  bsf  STATUS,RP0  ; bank 1
 BSF PIE1, TMR1IE ;Enable Timer 1 interrupt
  bcf  STATUS,RP0  ; bank 0
 BSF INTCON, PEIE ;Enable peripheral Interrupts
 BSF INTCON, GIE ;Emable global interrupts
  return
  
;*******************************************************************************
; Main Function
;*******************************************************************************
main:
 BCF INTCON, GIE ;Disable global interrupts

;
  call  init  ; initialize PIC registers

;
 
Wait_High:

WH_loop:
 BTFSS Pulse_in ;Wait for pulse to be low
 GOTO WH01
 GOTO WH_loop 

WH01:
 BTFSS Pulse_in ;Wait for pulse to be high
 GOTO WH01
 
WH02:
 CLRF  Retrig_Count

 BTFSS Flags,0
 CLRF Delay_Count 
 
 MOVLW Delay_On_Count
 SUBWF Delay_Count, W
 BTFSC STATUS, Z 
 GOTO WH03
 INCF Delay_Count
 BCF  GPIO, 0 ;Enable out 
 GOTO WH04

WH03:
 BSF  GPIO, 0 ;Enable out

WH04:
 BSF GPIO, 2
 BSF Flags,0
 GOTO WH_loop 




  end
It is set to cut off at 20 HZ and to cut back on when there have been 20 pulses that have been above 20 HZ
These equates at the start of the program set these values.
TMR1_Counts equ D'1000' ; Constant so that timer 1 interrupts every millisecond
Max_Retrig_Count equ D'50' ;Max number of milliseconds allowed between input pulses
Delay_On_Count equ D'20' ;Number of input pulses before Enable is set.
I have attached the .hex file to load into the PIC12F629.

Les.
 

Attachments

  • LF_Cutoff02.HEX
    396 bytes · Views: 103
Amazing that you would take the time on this. I'd do the same and learn assembly while I'm at it!

I've also been told about the 10F200 chip. I modified my c code to suit and it worked for the 200 only taking 50% of the capacity (for the 629 it's 11%). I've just got to find a SOP-6 adapter and it's a possibility.

Quite a few things on order now. I bought a Pickit 2 and only now realised that it's purpose is for use without a PC. Primarily I bought it to work on Linux.

Can I ask, what did you use to convert to hex?

Again, huge thanks for spending your time when you didn't have to.

Andrew
 
Hi Andrew,
If you Google PICKIT2 Linux it looks like there is software so you can use it with Linux. I use MPASM. I could not find a download source for it by itself so I had to install the MPLAB IDE. I think in the past I managed to just download older versions of MPASM. There is probably a PIC assembler available for Linux. It may be easier to find an old PC that someone is throwing out to use for programming PICs I have just started playing round with some ESP8266 modules so I will need to have another attempt at learning "C" as all the code written for them seems to be in "C" or LUA If you manage to get hold of an old PC I will see if I can put just the files needed to run MPSAM on my Dropbox public folder so you don't have to install the complete IDE.

Les.
 
I've already got it ready to go for when the pickit2 arrives as I have little time to make something that works before I head off touring!

I found the original source for the pickit2 and it still builds. For the compiler I ended up with Microchips XC8 which is free and works (without MPLAB IDE). I tried sdcc but the documentation was woeful and examples online were no longer applicable.
 
Final (I hope) circuit attached.
 

Attachments

  • usb.png
    usb.png
    36.8 KB · Views: 139
Hi All,

I'm nearly there, I rewrote the software and got the PIC working. It's perfect!

I'm going to use a mosfet bridge instead of skhotty's, I've implemented this using 2x AOP605's for now but will switch to IRF7389 for manufacture (same spec). It's working, however, even though my gate voltage didn't go above 15V I somehow blew the AOP605's. I've now stuck 2x 15k resistors inline to the joined gate and it's not happened since. Is it required to reduce the current going to the gate?

Andrew
 

Attachments

  • bridge.png
    bridge.png
    6.4 KB · Views: 126
Here's the final circuit. You can see how I've added the resistors to the gate out of paranoia!
 

Attachments

  • usb.png
    usb.png
    52.1 KB · Views: 125
Hi Andrew,
It looks like you are well on your way to a working prototype. I have not seen the mosfet bridge rectifier before. I should reduce the voltage drop in rectification. I do not fully understand how it works as the mosfets seem to be conducting in the opposite way to the way they normally conduct. I do not understand why you original mosfets failed. I do not see how the 15 K resistors will help unless you also have zener diodes on the gate to limit the voltage. (Mosfets have a very high input resistance so 15 K would not make much difference to the gate voltage. ) I assume you are using your own code written in "C" rather than my version. If so I would be interested to see it. I may be able to follow it to some extent even though I can't write code in "C"

Les.
 
Hi Les, sorry yes I had to redo the code. I've attached the working version.

With the Mosfets, they use the build in diodes for the traditional bridge setup but once the voltage get to the 4.5v mark they take over. I couldn't get it working the first time but now it is. No idea how I blew the first two either. I've tried to find MOSFET's with skhottys but to no avail. I'm also unsure if it'll make any difference, there would be a greater voltage drop up to 4V but that's irrelevant anyhow, it's 5V+ that is required. Possibility it means less current is available although the voltage is the same. Splitting hairs anyhow.

If I'm limiting the voltage to 17V AC, that's 17V per side which should be fine for the MOSFET right? I'm aware that for DC it's x1.4 but wasn't sure if this is the value I should be applying to the gate. Need to be 100% before I get the 5,000 board run off done!

Regards, Andrew
 

Attachments

  • 629v2.c
    1.6 KB · Views: 169
Hi Andrew,
I think I can see the basic flow of your program. This is what I think you are doing. I think you start by looking for any transition on GP1 input. (Either low to high or high to low) If it goes round the loop more than a certain number of times without seeing a transition you turn off the enable signal (GP0) What I don't understand is how you know how each time round the loop is. (If it was assembler you would know this from the time each instruction takes to execute but is there a way to know how long each "C" instruction takes to execute ?) I think you then have a delay before the enable signal goes high after the input frequency returns above the cut off value. In my code I counted the number of cycles of the input waveform to create the delay. This method was just emulating the way the hardware solution worked. Also I have been Googling to try to find out what the header file xc.h does. You are limiting the PEAK voltage of the AC waveform to 17 volts. (So I assume the two zener diodes are rated at about 16.3 volts (17 volts less about 0.7 volts for the forward volt drop of the other zener diode.) Are the zener diodes a high enough current rating to be able to clamp the output of the alternator ? If you have an oscilloscope it would be worth checking that with the alternator rotating at the maximum speed it will run at that the AC waveform is clamped to 17 volts peak. As you are clamping the peak voltage you do not need to multiply by 1.414. (This value is the ratio of peak voltage to RMS voltage of a sine wave.

Les
 
Rather than use a delay I calculated (trial and error) how long the chip takes to exec the instructions and came up with two numbers based on it (this is actually how the _delayms) does it. It's iteration of i and j is based on cpu frequency, fixed at 4mhz.

That's interesting about the zeners, I was using those values to give less than 24v (max buck voltage), but knowing that I can go to 23v zeners? Are you sure!? I'm using beefy 5W, and carefully heatsinking them. They do get hot, but they've stood up find so far. If I can make them 23V, even better. It's a simplier way than using a darlington shunt.

The mosfet's have ended in failure, I didn't realise that they reverse flow, so the capacitance goes back through them and keeps the detection pin on high. Whoops!

Andrew
 
Hi Andrew,
I don't think I have seen that simple method used before in "C" programs. It makes the program quite short and your timing requirement is not very critical. I didn't spot that problem with the mosfet bridge. I do not like running components on the limit so I would not use a zener above 20 volts. You could use a single zener on the output of the bridge rectifier rather than two back to back on the AC side. The disadvantage would be that the power dissipated in clamping the voltage would not be shared between two zener diodes. Also the bridge rectifier would have to carry the clipping current so you may have to use a higher rated one. The possible advantage would be that you could use a stud mounted zener diode which may be easier to heat sink. I am fairly confident that you do not have to consider the 1.414 factor. (Hopefully someone else will comment.)

Les.
 
Hi Les,

I originally did it a similar with a darlington after the bridge, but you're right the bridge diodes then have to carry more than the 1A currently.

You're right about the timing but then the small PIC's don't have timer interrupt in them so wasting cpu cycles is the only way. It works though!

I modified the code timing to only count on a signal of zero and removed the diode/resistor from one side. It fits's into a 10F200 at 75%.

And I modified the schematic to include on pcb programming, a 78L05 for the pic and revert back to standard bridge rectification.

It works fine with my dynamo. :)

I'm now ready to make the Gerber (i have the BOM and footprints). If you're available to do that (at cost of course) let me know, or if you can recommend anyone. I did have a look but there's too much room for a novice to cock-up!

Regards,

Andrew
 

Attachments

  • usb.png
    usb.png
    40.2 KB · Views: 120
Hi Andrew,
I've never used any of the PIC10F series devices so I did not know they did not have any timer modules until I looked at the data sheet today. Most things I build are built on Veroboard (Stripboard.) so I only use devices that are available in DIL packages. I have never produced files to get PCBs done. PCBs I have made in the past have been hand painted with cellulose paint and etched in ferric chloride solution. I have tried printing out PCB layouts from magazines but none of the laser printers I have had produced dense enough print to use with photoresist coated board. For this reason there has been no point in designing PCB layouts with Eagle or any other package.

Les.
 
Status
Not open for further replies.

Latest threads

Back
Top