Go Back   Home > Forums > >

Software Tools SPICE, PCB CAD, speaker design and measurement software, calculators

PIC programming
PIC programming
Please consider donating to help us continue to serve you.

Ads on/off / Custom Title / More PMs / More album space / Advanced printing & mass image saving
Reply
 
Thread Tools
Old 5th November 2015, 12:18 AM   #1
deafbykhorns is offline deafbykhorns  United States
diyAudio Member
 
Join Date: Aug 2004
Location: florida
Default PIC programming

Ok, I'm pretty good with circuits, cadd, design and layout but don't know squat about programming a pic and looking for some shortcuts.
Looking for some sample C-code that's been written for amp turn on, delay, voltage monitoring (a/d conversion). I think this may accelerate the process since I don't have the time to start from scratch.
Or should I just build the board and pay a programmer to write the code?
  Reply With Quote
Old 5th November 2015, 12:58 AM   #2
nigelwright7557 is offline nigelwright7557  United Kingdom
diyAudio Member
 
nigelwright7557's Avatar
 
Join Date: Apr 2008
Location: Carlisle, England
I don't have much C code but here is a assembler version of my cold start program
It is for a class d 2092 based amplifier.
It monitors VCC voltage and then controls a opto isolator that controls the reset pin on the 2092.

;INTH0.ASM
;**********
#include p16f506.inc
LIST R=DEC

;----- CONFIG Options --------------------------------------------------
;_OSC_LP EQU H'0FF8' ; LP oscillator and 18 ms DRT
;_LP_OSC EQU H'0FF8' ; LP oscillator and 18 ms DRT
;_OSC_XT EQU H'0FF9' ; XT oscillator and 18 ms DRT
;_XT_OSC EQU H'0FF9' ; XT oscillator and 18 ms DRT
;_OSC_HS EQU H'0FFA' ; HS oscillator and 18 ms DRT
;_HS_OSC EQU H'0FFA' ; HS oscillator and 18 ms DRT
;_OSC_EC EQU H'0FFB' ; EC Osc With RB4 and 1.125 ms DRT
;_EC_OSC EQU H'0FFB' ; EC Osc With RB4 and 1.125 ms DRT
;_OSC_IntRC_RB4EN EQU H'0FFC' ; INTRC With RB4 and 1.125 ms DRT
;_IntRC_OSC_RB4EN EQU H'0FFC' ; INTRC With RB4 and 1.125 ms DRT
;_OSC_IntRC_CLKOUTEN EQU H'0FFD' ; INTRC With CLKOUT and 1.125 ms DRT
;_IntRC_OSC_CLKOUTEN EQU H'0FFD' ; INTRC With CLKOUT and 1.125 ms DRT
;_OSC_ExtRC_RB4EN EQU H'0FFE' ; EXTRC With RB4 and 1.125 ms DRT
;_ExtRC_OSC_RB4EN EQU H'0FFE' ; EXTRC With RB4 and 1.125 ms DRT
;_OSC_ExtRC_CLKOUTEN EQU H'0FFF' ; EXTRC With CLKOUT and 1.125 ms DRT
;_ExtRC_OSC_CLKOUTEN EQU H'0FFF' ; EXTRC With CLKOUT and 1.125 ms DRT

;_WDT_OFF EQU H'0FF7' ; WDT disabled
;_WDT_ON EQU H'0FFF' ; WDT enabled

;_CP_ON EQU H'0FEF' ; Code protection on
;_CP_OFF EQU H'0FFF' ; Code protection off

;_MCLRE_OFF EQU H'0FDF' ; RB3/MCLR pin functions as RB3, MCLR tied internally to VDD
;_MCLRE_ON EQU H'0FFF' ; RB3/MCLR pin functions as MCLR

;_IOSCFS_OFF EQU H'0FBF' ; 4 MHz INTOSC Speed
;_IOSCFS_ON EQU H'0FFF' ; 8 MHz INTOSC Speed


;4mhz
__CONFIG _MCLRE_OFF & _WDT_OFF & _CP_OFF & _IntRC_OSC_RB4EN & _IOSCFS_OFF

;*****************************************
;SUBWF FLAGS
;IF W > MEM THEN NC
;IF W = MEM THEN C
;IF W < MEM THEN C
;IF W<= MEM THEN C

;IF MEM>=W THEN C
;IF MEM<W THEN NC
;*****************************************
#DEFINE OPTO PORTC,0 ;opto output
#DEFINE LED PORTC,1 ;led output

FIRSTRAM EQU 0XD ;16F506 RAM START ADDRESS

;10H TO 1FH IN BANK 0
CBLOCK FIRSTRAM
TEMP
TEMP1
TEMP2
LEDCOUNT

ENDC

ORG 0
CLRF FSR ;BANK 0 RAM
MOVWF OSCCAL

MOVLW 0
TRIS PORTC
CLRF PORTC

MOVLW 1
TRIS PORTB
CLRF PORTB

MOVLW 0X41 ;COMPARATORS OFF
MOVWF CM1CON0
MOVWF CM2CON0

CLRF VRCON

MOVLW 0XD0 ;DISABLE WEAK PULL UPS ON PORTB
OPTION



MOVLW 0XC1 ;SET UP ADC
MOVWF ADCON0


;WAIT 4 SECS ON POWER UP WITH LED ON STEADY
BSF OPTO
BSF LED
MOVLW 4
CALL WAITWSECONDS
BCF OPTO
BCF LED



MLOOP BSF ADCON0,GO ;START A2D
NOP
WAIT BTFSC ADCON0,GO
GOTO WAIT


;IF W > MEM THEN NC
;IF W = MEM THEN C
;IF W < MEM THEN C
;IF W<= MEM THEN C
MOVLW 134 ;15 volts before regulator
SUBWF ADRES,W
BC MLOOP

;BROWNED OUT
BSF OPTO ;opto on

;FLASH FOR 10 SECONDS IN CASE WAS SHORT TURN OFF
MOVLW 10*2
MOVWF LEDCOUNT
FLASH BSF LED
CALL WAIT250MS
BCF LED
CALL WAIT250MS
DECFSZ LEDCOUNT,F
GOTO FLASH

BCF OPTO
GOTO MLOOP



;******************************************
;63 max input
WAITWSECONDS MOVWF TEMP2
CLRC
RLF TEMP2,F ;*2
RLF TEMP2,W ;*4

GOTO SECS

WAIT1SECOND

MOVLW 4
SECS MOVWF TEMP2
L3
MOVLW 250 ;250MS LOOP
MOVWF TEMP1
L2
MOVLW 250 ;1 MS LOOP
MOVWF TEMP
L1 NOP
DECFSZ TEMP,F
GOTO L1

DECFSZ TEMP1,F
GOTO L2

DECFSZ TEMP2,F
GOTO L3
RETLW 0
;************************************
WAIT1MS MOVLW 250
MOVWF TEMP
W1M NOP
DECFSZ TEMP,F
GOTO W1M
RETLW 0
;***********************************
WAIT250MS MOVLW 250
MOVWF TEMP2
W2M CALL WAIT1MS
DECFSZ TEMP2,F
GOTO W2M
RETLW 0
;*************************************

END
  Reply With Quote
Old 5th November 2015, 07:09 AM   #3
Chartal is offline Chartal  Canada
diyAudio Member
 
Join Date: Nov 2004
Location: Montreal
Hi.

Maybe you can start with Arduino.

www.arduino.cc
  Reply With Quote
Old 5th November 2015, 10:33 PM   #4
deafbykhorns is offline deafbykhorns  United States
diyAudio Member
 
Join Date: Aug 2004
Location: florida
Yeah, I'm totally freaking lost, Here's my half *** attempt (it's not finished). I might as well pay someone to write it. I love designing and building tube amps but absolutely hate this programming. I have a severe headache now.... I can program lighting controllers with simple logic but this is on another level.
I want relay1 to come on when power button is closed, after 30seconds turn on relay2, then in 5 seconds turn on relay3
After relay3 start checking ADC input 2 (TRISL.1) for correct input (greater than or less than) If outside range turn off all relays (until power switch is cycled on and off again)


'************************************************* ***************
'* Name : Voltage monitor.BAS *
'* Date : 7/18/2015 *
'* Version : 1.0 *
'* Notes :This program allows you to choose a voltage to *
'* :turn on an led or a relay *
'* : Uses PIC12F675 *
'************************************************* ***************

'PicBasic Pro required defines
ANSEL = 0
CMCON = 7

' Setup pot
DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50

pz1 var word ' Delay pause for loop
relay1 var PORTL.5 'output relay on pin 2 You can now call pin 2 relay1
relay2 var Portl.2 'output relay on pin 5 You can now call pin 5 relay2
relay3 var PORTL.3 'output led pin 3 Create a varable called relay3

'set up registers/pins

' set directions in TRIS register is pin an input (1) or an output (0)

'inputs
TRISL.0=1 'the switch is on pin 7
TRISL.1=1 'Select on pin 6, a wire to chose which relay the Switch controls

'(outputs) Relays on pin 2, 3 and 5
TRISL.2=0 'relay1 Pin 5
TRISL.5=0 'relay2 Pin 2
TRISL.3=0 'relay3 Pin 3
pz1 = 50

loop1: 'Start of program

' 0 = gnd 1 = +5
IF portL.0 = 0 and portL.1 = 0 then relay1 = 0 ' turn relay1 off
IF portL.0 = 0 and portL.1 = 0 then relay1 = 0 ' turn relay2 off
IF portL.0 = 0 and portL.1 = 0 then relay1 = 0 ' turn relay3 off
IF portL.0 = 1 and portL.1 = 0 then relay2 = 1 ' turn relay1 on
if portL.3 = 1 then start timer ' not sure how to program this part

pause pz1


goto loop2:


loop2:

' read pot if not in range turn off relay 3
ADCON0=2
ADCIN 0,adval 'Use the ADC to read Read AN0(pot)& store it in the var "adval"

if adval < 160 or > 190 then relay3 = 0 ' test voltage trip point 0 to 256
relay3 = 0 ' turn off relay
Pause pz1
goto loop1 ' return and read pot again


goto loop1: 'Return to start of program do again
  Reply With Quote
Old 5th November 2015, 10:39 PM   #5
deafbykhorns is offline deafbykhorns  United States
diyAudio Member
 
Join Date: Aug 2004
Location: florida
Quote:
Originally Posted by Chartal View Post
Hi.

Maybe you can start with Arduino.

www.arduino.cc
Doe's Aurdino have a software that writes the code from a simple input?
I'm used to lighting software that does something similar to below.

24. // Garage Door Controller
// Blocks 25 - 29 check garage door when armed away
//
// Blocks 30-34 check garage door during the week 9am-5pm to make sure garage is closed in
// case system is not armed
// Motion detector will check motion to see if Susan is working in the yard before closing
// (30 minute motion flag)
//
// Blocks 35-39 check the garage door all week when its dark
// Motion detector will check motion before closing
//
// Blocks 40-41 makes sure the relay in not latched more than one second
25. WHEN AWAY
AND IF Garage Door NOT READY
THEN Garage Motion Flag ON FOR 90 SECONDS
THEN Front Motion Flag ON FOR 90 SECONDS
26. WHEN Garage Motion SECURE
AND IF Garage Door NOT READY
AND IF AWAY
THEN Front Motion Flag ON FOR 90 SECONDS
THEN Garage Motion Flag ON FOR 90 SECONDS
27. WHEN Motion Outside Front Garage SECURE
AND IF Garage Door NOT READY
AND IF AWAY
THEN Front Motion Flag ON FOR 90 SECONDS
THEN Garage Motion Flag ON FOR 90 SECONDS
28. WHEN Front Motion Flag OFF
AND IF ARMING AWAY
AND IF Garage Door NOT READY
AND IF Garage Door Flag OFF
AND IF Garage Motion Flag OFF
THEN Garage Door Flag ON FOR 15 SECONDS
THEN SAY GARAGE DOOR CLOSE IN TEN SECONDS
29. WHEN Garage Door Flag OFF
AND IF ARMING AWAY
AND IF Garage Door NOT READY
AND IF Front Motion Flag OFF
AND IF Garage Motion Flag OFF
THEN Garage Door Controller Relay ON FOR 1 SECOND

Last edited by deafbykhorns; 5th November 2015 at 10:41 PM.
  Reply With Quote
Old 5th November 2015, 10:40 PM   #6
nigelwright7557 is offline nigelwright7557  United Kingdom
diyAudio Member
 
nigelwright7557's Avatar
 
Join Date: Apr 2008
Location: Carlisle, England
I found the best way to write a program is to do it incrementally.

Do the first step and run it on your hardware.
Have a LED flash if the software worked OK.
If it doesn't do what you expected then you only have one piece to have to work on which makes it easier.

Or just use MPLAB simulator to test your software.

I bought a cheap PICKIT3 from China. Works fine.
  Reply With Quote
Old 5th November 2015, 10:48 PM   #7
tomchr is offline tomchr  Canada
diyAudio Member
 
tomchr's Avatar
 
Join Date: Feb 2009
Location: Calgary
The Arduino uses an interpreter that runs on the micro controller. It's sorta like the old GW-Basic or Comal-80 on a PC. For simple tasks that's just fine. If you have any programming experience at all, you may want to skip to C directly, though.

I personally hate the PICs with a passion due to their architecture. Specifically, the "we support everything, just not what you want to do" when it comes to I/Os.

I much prefer the Atmel AVR series. Atmel provides the IDE and assembler. Google WinAVR to find a C cross-compiler.

Another interesting option is the NXP Cortex M0 processors. Some of them are less than $1 in QTY = 1 and the dev kits are dirt cheap. The development tools are free downloads from NXP.

If this is your first time with embedded programming, I suggest starting simple. My first program is usually one that increments an 8-bit variable, continuously. So you'll get an 8-bit value that starts at 0, increments to 255, and overflows back to 0. Repeat until power is turned off. Then output this variable on an 8-bit port. With an o'scope you can now pick up square waves of increasing frequencies when measuring from the MSB to the LSB. Once I'm there, I know that I can talk to the chip and make a program run. From there, it's "just" a matter of coding the end application.

Tom
__________________
Modulus-86, 186, 286, & 686: 40-240W (8Ω), <-120dB THD. HP-2 Headphone Amp: 660mW, <-130dB THD. Taming the LM3886.
Neurochrome : : Audio - www.neurochrome.com - Engineering : : Done : : Right
  Reply With Quote
Old 8th November 2015, 12:34 AM   #8
deafbykhorns is offline deafbykhorns  United States
diyAudio Member
 
Join Date: Aug 2004
Location: florida
Default What I've learned today

What I've learned today is PICs are a PITA.
On my 4th lesson on Aurduinos and this is much easier to program than the PIC.
Many development boards as well so I don't have to make a PCB. Thanks for pointing me in the right direction. There are some Aurduino boards on Ebay with 4 relays built in. This will allow me to have a little more control on the startup and shutdown sequence of my tube amp. This is going to be fun....
  Reply With Quote
Old 8th November 2015, 08:53 PM   #9
geraldfryjr is offline geraldfryjr  United States
diyAudio Member
 
Join Date: Feb 2008
Location: Jackson,michigan
Hi there!!

I know of another PIC alternative that you may be interested in.

It uses the 28 pin DIP PIC32MX150, 170 or 250/270 series of controllers.
It is called the Micromite from Geoff's project's and runs Basic via serial terminal programmed all within the chip itself.

It has some GPIO and supports SPI an I2C as well with in the MMBASIC interpreter for items such as digtal pots and switches.

LCD display's are easily connected and supported with in the OS too.

Just in case that you need more GPIO and a larger display the lastest version uses the PIC32MX470.
I know that the SD1963 and HD44780 LCD controllers are supported for all of the versions as well as a few others.

As long as you already have a Pickit3 you can just flash the chip with the firmware and you are good to go too communicate to the chip and program it in MMBASIC via serial terminal intarface to your computer.
Or you can also the MCP2200 USB to serial converter from Microchip.

It is a very simple and impressivly powerful little system and very inexpensive to do.

Here are the links,

Geoff's Projects - The Micromite Official WEB Page

And the Supporting Forum where you will find the latest versions and variations,

The Back Shed: Microcontroller and PC projects

Cheers!!

jer

Last edited by geraldfryjr; 8th November 2015 at 09:02 PM.
  Reply With Quote
Old 12th November 2015, 12:07 AM   #10
deafbykhorns is offline deafbykhorns  United States
diyAudio Member
 
Join Date: Aug 2004
Location: florida
Default My first attempt at programming an ATMEL

Should have my relay boards in a couple weeks for testing my first attempt at coding. Im a little unsure about the softstart sequence after // softstartdelay. I think once relay 1-3 is on it wont matter until the pushbutton is opened.

/*
Soft start relay Arduino
Future mods include voltage monitor at cathode of GM70 using ADC

*/

// Relay setup, relay1=filaments relay2=softstart relay3=HV on
// give it a name:
int relay1 = 9;
int relay2 = 10;
int relay3 = 11;
const int buttonPin = 2; // the number of the pushbutton pin

// the setup routine runs once when you press reset:
void setup() {
//initialize the digital pins as an output.
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn Relay1 on:
digitalWrite(relay1, HIGH);
} else {
// turn Relays off:
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
// softstartdelay
pinMode(relay1, HIGH); // start time delay
delay(90000); // wait for a 90 seconds
digitalWrite(relay2, HIGH); // High voltage soft start relay
delay(3000); // wait for a 3 seconds
digitalWrite(relay3, HIGH); //High voltage relay on


}
}
  Reply With Quote

Reply


PIC programmingHide this!Advertise here!
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Up to date PIC programming TroelsM Everything Else 4 6th May 2009 08:54 PM
PIC Programming & PICKIT2 Anyone used one- Vista ? Mooly Parts 37 1st December 2008 06:24 AM
PIC programming serosmaness Digital Source 12 6th February 2005 10:33 PM
PIC programming future Parts 33 4th June 2003 04:26 AM


New To Site? Need Help?

All times are GMT. The time now is 01:04 AM.


Search Engine Optimisation provided by DragonByte SEO (Pro) - vBulletin Mods & Addons Copyright © 2021 DragonByte Technologies Ltd.
Resources saved on this page: MySQL 14.29%
vBulletin Optimisation provided by vB Optimise (Pro) - vBulletin Mods & Addons Copyright © 2021 DragonByte Technologies Ltd.
Copyright ©1999-2021 diyAudio
Wiki