PIC programming

Status
This old topic is closed. If you want to reopen this topic, contact a moderator using the "Report Post" button.
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?
 
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
 
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
 
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:
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.
 
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
 
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....
 
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!! :D

jer :)
 
Last edited:
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


}
}
 
Hi,
No too long ago I was writing a program for the Arduino UNO board to turn some relays in sequence but I put it away to worked in another project. I modified it so you can use it in your project by adding an analog input that will be monitoring continuously and if the voltage input reached the set point it will turn off all the relays immediately and wait until the power switch it is turning off/on. Attached it is the program file in text and a drawing showing the pins used in the program. The only thing you have to do it is modified it to match your pin numbers and the delays for each relays 1 ,2 and 3.
 

Attachments

  • AC sequencer.txt
    2.5 KB · Views: 41
  • ac_sequencer.jpg
    88.9 KB · Views: 60
Thanks a bunch! should be getting boards late next week.
I'm assuming the analog_val = analog_val *.0049 is basically 5 volts so you can set the flag to a voltage instead of using a 0-1023 range?
That's pretty cool and saves you from punching the calculator every time you want to set a value!
I calibrated my center value at 3 volts or 612 ADC value so the low value would be 591 (2.9v) and the high value would be 633 (3.1v).
So should I do this in two lines or can a do a range?

if (analog_val <= 2.9) flag = 1;
if (analog_val >= 3.1) flag = 1;

Or this? I think || is the same as "or"

if ((analog_val < 2.9) || (analog_val > 609)) flag= 1;


Seems easy once someone shows you all the shortcuts. I learn more by watching or reading the forums than opening up a book......

I see myself going nuts adding features with this Arduino....
Maybe a display on startup showing voltages, delay countdown and status of relays
 
Last edited:
Hi,
If you want to use floating point for the set point like 2.1 and 3.9 then you must change the ana_inp variable to floating point and then convert it to integer like 21 and 30 by multiplying the floating point by 10.0. This will shift one decimal number to the left.
float analog_val = 0;
int float_int = 0;

and change the program as follow:

analog_val = (analog_val *.0049)*10.0; //Convert input counts to floating point voltage value
float_int = analog_val; //convert value from floating point to integer
Then instead use 2.1 use 21 and 31

if (float_int >= 29 && float_int <= 31) flag = 1; // value to use is 29 and 31

Attached it is the updated program
Hoped you understand the above explained. I am a lousy technical writer.
 

Attachments

  • AC_sequencer_11_17_2015.txt
    2.7 KB · Views: 27
Maybe you can start with Arduino........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.

Arduino is a compiled language. The Arduino IDE (Integrated Development Environment) is the program that runs on the PC, MAC or Linux box where you write and debug your program in a simple language. Arduino calls your program a "sketch".

Once the sketch is complete, you "upload" it to the Arduino board. At this time the sketch is compiled into machine language (hex code) and sent over a serial port (usually USB) to the Arduino board. There is a "bootloader" program that runs on the board that allows for flashing the program into flash memory without needing a programming board like the PicKit.

The Arduino sketch language IS a simplified version of "C" that leaves out some of the features like pointers that cause the most headaches in beginners. The Arduino compiler in the IDE IS however a full blown C compiler which will eat C and C++ code, and compile it without error. You can get started with Arduino, and transition to C without changing anything.

I personally hate the PICs with a passion due to their architecture

I worked for Motorola for 41 years, so I started with the MC6800 in 1975. Like the PIC, the Mot chips are different from the usual Intel architecture that many programmers are used to.

In 1993 I designed a simple circuit that needed very fast processing and I/O, faster than any cheap processor on the planet could do. I discovered a startup called Microchip (actually a spin off of General Instrument). They had a simple chip called the PIC16C54 (about $1.50 at the time) that, with a 20% overclock did exactly what I needed.

I have used whatever processor chip I can get, from whichever chip vendor, that best fits the application, AS LONG as the development tools are cheap, or preferably, free.

Yes, the older 8 and 16 bit chips are "different" but sometimes they are just the right part. The dsPIC chips have their unique uses, especially software SMPS's, and the new 32 bit parts are more like everybody else's 32 bit parts. They use the MIPS core rather than the usual ARM core though.

In the beginning all the Arduino boards used simple 8 bit Atmel chips. Within a year or two there were boards based on more powerful chips, and chips from vendors other than Atmel. There are dozens of Arduino compatible boards out there today with processing power from simple 8 bit to "Pentium class".

I have been using these since the eat my simple code without complaint, and come in several speed and size ranges for a reasonable price. Yes, they are PIC chip based.

Digilent Inc. - Digital Design Engineer's Source

Note, Atmel is now the target of a proposed acquisition. They may be acquired by Dialog, a company that is much smaller than Atmel. Like the TI acquisition of National Semi, the result WILL be that some Atmel people will lose their jobs, and some Atmel parts will go extinct....It always seem to happen that way.
 
Hi,
Yes, you can use the counts but will loose resolution. Floating point will give a better accurate input reading. Just write the value you want without the decimal point. Like you want 3.1 volt write 31. But it is your decision which one it is better for you.

Thanks, I prefer the resolution. There's a lot to this programming...
I would of thought that the counts would be more accurate or at least equal to.
Found this definition on the Arduino site.
"Floating-point numbers are often used to approximate analog and continuous values because they have greater resolution than integers. "

Note, Atmel is now the target of a proposed acquisition. They may be acquired by Dialog, a company that is much smaller than Atmel. Like the TI acquisition of National Semi, the result WILL be that some Atmel people will lose their jobs, and some Atmel parts will go extinct.
Might want to stock up on some spare UNO boards....
I was attracted to the PIC for its simplicity and minimalist inputs/output pins but the programming information and pre-made development boards on the internet are not even close to the Atmel. I was going to custom build a PIC/relay board and found the Atmel with relays on the same board already made, just add inputs. Perfect for the hobbyist
 
Last edited:
Hi,
I do not worry too much because you can switch to the Texas Instrument MSP430 micro processor since it use Energia editor that it is a copy of Arduino UNO editor.It will compile and run programs written for Arduino UNO. I just bought one of their lunch pad board. It is a nice board and the cost just start at $9.99 dollars. I think these is to compete with Arduino.
Here it is the link so you can check it out: TI LaunchPad - LaunchPads
 
Status
This old topic is closed. If you want to reopen this topic, contact a moderator using the "Report Post" button.