WWVB Time Signal Generator Test w/ HEX

[Edit: I’ve added the .asm code in the “Code” page in the menu above. Below you can find the .HEX file for easy programming]

A successful test on my WWVB signal generator. I’m going to shy away from calling it a transmitter because I don’t think there is any allowances for any broadcasting on 60KHz, so to stay legal I would imagine you’d need to conform to part 15, shield everything, use an attenuator and dump the signal into a shielded box with the clock? I somehow doubt the FCC checks up on sub-mW transmissions on 60KHz though.

The concept is simple, 1 baud rate transmission of a 60 frame packet. The amplitude shift keying (ASK) system WWVB transmits is recreated using a CMOS CD4066 switch. Dump a 60KHz sine wave (keeping in mind maximum input/output signal specifications) into one of the switch ports. Use the output on PORTB.0 of the PIC to control the switch and the other side of the switch goes to you device under test (I used a couple feet of wire as an antenna and just placed the wire in the neighborhood of the clock receiver). It’s a no brainer. Check out the NIST site on WWVB if you want more details. I’ll probably re-port the code to a 12F629 when I get my new PICKit3 in and I’ll likely build a board with a 60KHz generator.. maybe I’ll even sell it on Tindie if I’m feeling ambitious.

The Test! I was getting my ass kicked earlier this week as my circuit was not working and it seemed like everything was just right. It did force me to really tweak my timing to make it within my range of error on being able to measure the exact pulse widths, I don’t know how precise it has to be but I assume that’s up to the algorithm decoding the signal. Turns out it wasn’t my code or circuit.. My $15 Fred Meyer “black friday special” atomic clock doesn’t work. It won’t receive the real WWVB (set up aligned with Boulder, CO away from electronics, blah blah). I was getting the proper signals out of the module, so I yanked the module out of the clock and hooked it up to a receiver designed by N0QBH. I mirrored his project here. He has a website for the project here. I used his schematic, ditched the need for the LCD and just grabbed my data off the RS232. Done! You can see a before and after output screenshot in the photos below.

My WWVB signal generator code (HEX) for a 16F628A is found here . Is it lame of my just to provide the HEX? yeah…. but all you need to do is hook up PORTB.0 to switching input of a 4066 with a signal generator feeding a 60KHz sine wave and you’re in business. (And a resistor pulling /MCLR (PORTA.5) up as well if that wasn’t obvious? I’m using the internal oscillator; no xtal needed).  You are stuck with my fixed date of course.. which is why you want my assembly code right? No problem. Just ask… really (comment or e-mail). I don’t want to post it because I don’t really like comment trolls. This code is super BETA but at an acceptable starting point. Lots of opportunity for optimizing it as well.  Why didn’t I improve on this code? Because I don’t need to. I’m just using it to test receivers I’ve purchased from the UK and I’ll be working on a project with those in a little while.

My time/date is static here is a snippet of the main line code:

    CALL    MARKER                      ;MARKER FRAME REFERENCE BIT
    CALL    ONE                         ;40min
    CALL    ZERO                        ;20min
    CALL    ZERO                        ;10min
    CALL    ZERO                        ;Reserved
    CALL    ZERO                        ;8mins
    CALL    ZERO                        ;4mins
    CALL    ONE                         ;2mins
    CALL    ZERO                        ;1mins
    CALL    MARKER                      ;MARKER 1

… and so for some photos

signal from PIC before ASK modulation accomplished by switching a CMOS CD4066 with a 60KHz sine wave from a frequency generator.
signal from PIC before ASK modulation accomplished by switching a CMOS CD4066 with a 60KHz sine wave from a frequency generator.

 

WWVB receiver module removed from clock

The clock with the module removed, luckily they printed the pin diagram on the board. 5V, Gnd, PON, and TCO

WWVB Signal Generator and Receiver on Breadboard

Both the generator and receiver on the breadboard. The transistors form the RS-232 driver for the receiver.

Before and after on the RS232 output of the WWVB receiver
Before and after on the RS232 output of the WWVB receiver

 

WWVB Transmitting Generator/Simulator

[Edit: a follow up post includes code]

I owe it to my PICKit3 to give it a proper obituary.

PICKit3-BUR123467218, 1, Side-kick PIC programmer worked long and faithful.  Assisted in countless re-downloads to facilitate troubleshooting poorly written software.  PICKit3 met it’s maker when inadvertently electrocuted with 12VDC. Leaves behind distraught hobbyist programmer companion. You’re gone but not forgotten.

I was finishing up final touches on a 16F629 8 pin PIC that was generating a WWVB broadcast to have a piece of test equipment which will help me write code to receive WWVB ASK time/date information. I accidentally plugged my breadboard into 12VDC. Even with ninja speed I couldn’t get it unplugged in time. The 16F629 and PICKit3 were dead in milliseconds. I had to dig out the PICKit2 to save my weekend. Turns out the only PICs I have that will work with the 2 is a PIC16F628A and a 18F452. So I ported the code to the F628A and tested away on it. I have the code down to what I think is nearly perfect, with nearly perfect timing. I have tapped into the TCON output of a 15$ atomic clock I purchased recently. The clock I receiving the data fine, but it won’t sync for some reason. Two nights in a row with no synchronizing to the actual WWVB so I’m wondering if my 15$ clock just doesn’t work.

I’ll post the code later when I am sure it works. The receiver is on its way from the UK. That’ll be a project sooner than later. I was considering making a breakout/dev board out of the generator but then I saw a seller on Tindie has done that but also married it with a RTC.

The programming for the WWVB was pretty straight forward. Just look it up and capture it from NIST, no reason to repost, but here is a link to the PDF.

EDIT: Ugh, I got retweeted a few times so now I have that I-didn’t-post-code guilt. *IF* you want the code: comment, tweet or e-mail..  I’ll give it to you. I don’t want to post it for free download until I know for certain it’s reliable. I hate to steer someone wrong. Let me know if you want the .HEX file or just the assembly file and you can compile it yourself. I’ll even consider pre-programming a 16F series PIC if you really want it.

Right now the 60KHz transmitter is a function of my signal generator but the parts are on the way to build a solid low power transmitter. Anyone interested in the whole thing on a PCB?

My new PICKit3 and a couple goodies are already ordered along with some goodies…midweek until I can get back to work I suppose.

“To be continued”.

TYMKRS Rotary Encoder + TAUTIC 20 Pin Dev Board PIC Test/Code

I got an order from Tindie last night. I had issues with my DIY stepper based rotary encoder so when I saw TYMKRS “Turn Me v1” kit I had no hesitation parting with $10. It seemed to only come with Arduino sample code so I wrote my own code. My code is written in assembly for the PIC because that’s my preferred microcontroller and I’m not much of a C programmer. I happened to get another pair of Jayson Tautic’s 20 Pin Development Boards. Check out his Tindie store.. he has a bunch of interesting things. If I still lived in the midwest I’d pick up that lightning detector. (Jayson, if you ever read this your fan’s request more awesomness)

These wasn’t a ton of sample code online … some 16F88 code I didn’t care for… 16F84 code that was wretched… and some interrupt driven code I wasn’t feeling either so I wrote my own polling code from scratch. It’s commented enough to get what I was doing. The schematic should be fairly obvious, not counting the LED resistors I used a pull down resistor on the switch built into the encoder.

To my knowledge, while writing the code, I had my very first stack overflow bug!

I received the rotary encoder yesterday and I had another pair of Tautic's 20 pin development boards show up the day before. The 20 pin dev board comes with a Microchip PIC 16F1509.
I received the rotary encoder yesterday and I had another pair of Tautic’s 20 pin development boards show up the day before. The 20 pin dev board comes with a Microchip PIC 16F1509.
A few pieces, this went together in a few moments. The unlabeled connector side of the board is just support.
A few pieces, this went together in a few moments. The unlabeled connector side of the board is just support.

The important stuff:

;*******************************************************************************
;   Rotary Encoder Test, 2 bit                                                                              *
;   http://www.iradan.com
;
;   RA0:    OUT     TEST LED
;   RA1:    IN      ENC_B INPUT, NOTE: DISCONNECT OR INSURE LOW WHEN PROGRAMMING
;   RA2:    IN      ENC_A INPUT
;   RB5:    OUT     SWITCH STATUS LED
;   RB7:    IN      SWITCH INPUT
;   RC<0:7> OUT     ENCODER "COUNT"
;
;   VERSION 0.1     INITIAL CODE
;
;*******************************************************************************

    errorlevel -230, -302, -303, -313
    LIST R=DEC

#include "p16f1509.inc"

    __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _CLKOUTEN_OFF
    __CONFIG _CONFIG2, _LVP_OFF & _STVREN_ON

            UDATA_SHR
COUNT       RES .1
ENC_LAST    RES .1
ENC_CURRENT RES .1

#DEFINE ENCA    PORTA,1
#DEFINE ENCB    PORTA,2
#DEFINE ENC_SA  ENC_LAST,0
#DEFINE ENC_SB  ENC_LAST,1
#DEFINE ENCA_NOW    ENC_CURRENT,0
#DEFINE ENCB_NOW    ENC_CURRENT,1

;*******************************************************************************
; Reset Vector
;*******************************************************************************

RES_VECT  CODE    0x0000            ; processor reset vector
    GOTO    START                   ; go to beginning of program

;*******************************************************************************
; MAIN PROGRAM
;*******************************************************************************

MAIN_PROG CODE                      ; let linker place main program

START
    CALL    INIT

    BANKSEL PORTA
    BSF     PORTA,0         ;THIS WAS FOR TESTING.

    GOTO    LOOP

INIT
    CLRF    COUNT

    BANKSEL PORTC
    CLRF    PORTC
    BANKSEL LATC            ;Data Latch
    CLRF LATC               ;
    BANKSEL ANSELC          ;
    CLRF ANSELC             ;Digital IO
    BANKSEL TRISC           ;
    MOVLW   B'00000000'     ;RC<0:7> OUT
    MOVWF   TRISC

    BANKSEL PORTA           ;
    CLRF PORTA              ;Init PORTA
    BANKSEL LATA            ;Data Latch
    CLRF LATA               ;
    BANKSEL ANSELA          ;
    CLRF ANSELA             ;digital I/O
    BANKSEL TRISA           ;
    MOVLW B'00111110'       ;Set RA<0,6:7>out RA<1:5> in
    MOVWF TRISA             ;

    BANKSEL TRISB
    MOVLW   B'11011111'     ;RB<5> OUT, RB<0:4>,<6:7> IN
    MOVWF   TRISB
    CLRF    PORTB

   BANKSEL OSCCON           ;SET OSCILLATOR SPEED
    MOVLW   0x78            ;01111000  / 16MHz
    MOVWF   OSCCON

    CLRF    ENC_CURRENT     ;HOUSE KEEPING
    CLRF    ENC_LAST

    RETURN

LED
    BANKSEL PORTB           ;THIS TURNS ON SWITCH STATUS LED
    BSF PORTB,5
    RETURN

INCR
    INCF    COUNT,f
;   GOTO    RESUME
    RETURN

DECR
    DECF    COUNT,f
;    GOTO    RESUME
    RETURN

DETERMINE_DIRA
    BTFSC   ENCB_NOW        ;OKAY WHICH WAY DID IT TURN?
    CALL    INCR            ;INCREMENT
    BTFSS   ENCB_NOW
    CALL    DECR            ;DECREMENT
    RETURN

TESTLASTA
    BTFSS   ENC_SA          ;SO ENCA WAS HIGH, WAS IT LAST TIME?
    CALL    DETERMINE_DIRA  ;YEP..
    NOP                     ;GUESS NOT, RETURNING
    RETURN

TEST_ENC                    ;I'M ONLY TESTING FOR A HIGH ON ENC A.
    BTFSC   ENCA_NOW        ;THAT MEANS EVERY OTHER TICK ON THE ENCODER
    CALL    TESTLASTA       ;DOES NOTHING
    RETURN

LOOP
    NOP

    BANKSEL PORTB
    BCF PORTB,5             ;TURN OFF SWITCH STATUS LED
    BANKSEL PORTA
    BTFSC   PORTA,5         ;TEST IF SWITCH IS PUSHED
    CALL    LED             ;BRANCH IF ON, SKIP IF NOT
    NOP

    BCF ENCA_NOW            ;CLEAR CONTENTS OF ENCODER "NOW BITS"
    BCF ENCB_NOW

    BANKSEL PORTA
    BTFSC   ENCA
    BSF     ENCA_NOW        ;ENC_A INPUT IS HIGH
    NOP
    BTFSC   ENCB
    BSF     ENCB_NOW        ;ENC_V INPUT IS HIGH

    CALL    TEST_ENC        ;TEST ENC_A

    BANKSEL PORTC           ;DUMP ENCODER COUNT ONTO PORTC
    MOVFW   COUNT
    MOVWF   PORTC

    BCF     ENC_SA          ;SAVE STATUS OF ENCODER A & B INPUTS
    BTFSC   ENCA_NOW
    BSF     ENC_SA

    BCF     ENC_SB
    BTFSC   ENCB_NOW
    BSF     ENC_SB

    BANKSEL PORTA           ;USED FOR TESTING
    BCF     PORTA,0

    GOTO LOOP               ;LOOP FOREVER

    END

 

Quick and dirty Hello World, PIC 16F1509 + 4 Bit 44780 format LCD

Okay… nothing magical here. Just a boring old “Hello World”. There doesn’t seem to be any sample code for the newer enhanced mid-range PICs with a LCD that I could find online; perhaps you can find something better with Google than I will.

I promise you two things with this code: It’s fresh off the press so it’s ugly, sloppy, poorly commented, I didn’t use pagesel at all, it’s probably buggy as sh%t and it’s slow. Remember 300 baud modems and BBS chat rooms? yeah…. déjà vu. I was having problems with timing and I don’t have a proper logic analyzer and I didn’t care to do all the math.. so I went safe. In fact after pasting the code I see a huge obvious flaw as it is, you’ll see it, but anyways it’ll display Hello World. Enjoy.

Full disclosure: I was heavily inspired by this code as a building block from piclist.. mostly I wanted a quick and dirty way to go 4 bit.

lcd_16f1509

;*******************************************************************************
; PIC 16F1509 LCD Test
; http://iradan.com
;
; ver 0.01 Hello World 4 bit, extra-dirty.
;
;*******************************************************************************
errorlevel -230, -302, -303
 LIST R=DEC
#include "p16f1509.inc"
 __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _CLKOUTEN_OFF
 __CONFIG _CONFIG2, _LVP_OFF & _STVREN_ON
;*******************************************************************************
;Device 16F1509
;
; PIN DIAGRAM
;
; RA0 O x RC0 O LCD_1/11
; RA1 I x RC1 O LCD_2/12
; RA2 I x RC2 O LCD_3/13
; RC3 O LCD_4/14
; RA4 O x RB4 I RC4 O LCD_E/6
; RA5 O x RB5 O x RC5 O ?FUTURE _ PWM?
; RB6 I RC6 O 
; RB7 I RC7 O LCD_RS/4
;
;*******************************************************************************
;
GPR_VAR UDATA
R_SEND_TMP RES 1
R_SEND_W_TMP RES 1
R_WDATA_TMP1 RES 1
R_WDATA_TMP2 RES 1
int_delay1 RES 1
;*******************************************************************************
; Reset Vector
;*******************************************************************************
RES_VECT CODE 0x0000 ; processor reset vector
 GOTO START ; go to beginning of program

;*******************************************************************************
; MAIN PROGRAM
;*******************************************************************************
MAIN_PROG CODE ; let linker place main program
banksel OSCCON
 movlw b'01111000' ; INTOSC 16MHz
 movwf OSCCON ;
 banksel OSCSTAT ;
 btfss OSCSTAT, HFIOFR ; Running?
 goto $-1 
 btfss OSCSTAT, HFIOFS ; Stable?
 goto $-1
goto START
;-------------------------------------------------------------
w_to_data
 BANKSEL PORTC
 movwf R_WDATA_TMP1
 movf PORTC, W
 movwf R_WDATA_TMP2
 bcf R_WDATA_TMP2, 0
 bcf R_WDATA_TMP2, 1
 bcf R_WDATA_TMP2, 2
 bcf R_WDATA_TMP2, 3
 btfsc R_WDATA_TMP1, 0
 bsf R_WDATA_TMP2, 0
 btfsc R_WDATA_TMP1, 1
 bsf R_WDATA_TMP2, 1
 btfsc R_WDATA_TMP1, 2
 bsf R_WDATA_TMP2, 2
 btfsc R_WDATA_TMP1, 3
 bsf R_WDATA_TMP2, 3
 movf R_WDATA_TMP2, W
 movwf PORTC
 return
pulse_e
 BANKSEL PORTC
 bsf PORTC,4
 call delay2
 BANKSEL PORTC
 bcf PORTC,4
 call delay2
 return
send_w
 movwf R_SEND_W_TMP
 swapf R_SEND_W_TMP, F
 movlw 0x0F
 andwf R_SEND_W_TMP, W
 call w_to_data
 call pulse_e
swapf R_SEND_W_TMP, F
 movlw 0x0F
 andwf R_SEND_W_TMP, W
 call w_to_data
 call pulse_e
 return
delay2
 BANKSEL int_delay1
 MOVLW 0x01
 MOVWF int_delay1
OLOOP
 BANKSEL TMR0
 CLRF TMR0
 BANKSEL INTCON
 BCF INTCON,T0IF
 MOVLW 0xC0 ; PortB pull-ups are disabled, (c3=1:16)
 MOVWF OPTION_REG ; Interrupt on rising edge of RB0
 ; Timer0 increment from internal clock
 ; with a prescaler of 1:4.
; The TMR0 interrupt is disabled, do polling on the overflow bit
T0_OVFL_WAIT2
 BTFSS INTCON, T0IF
 GOTO T0_OVFL_WAIT2
NOP
 BANKSEL int_delay1
 DECFSZ int_delay1,1
 GOTO OLOOP
 NOP
 ;... else
RETURN
START
BANKSEL PORTA
 CLRF LATA
 BANKSEL PORTB
 CLRF LATB
 BANKSEL PORTC
 CLRF LATC
 CLRF PORTC
 BANKSEL ANSELC ;
 CLRF ANSELC ;Digital IO
 BANKSEL PORTC ;
 CLRF PORTC
 BANKSEL TRISC ;Set all PORTC to outputs
 CLRF TRISC
 BSF PORTC,6

movlw b'11000111' ;configure TMR0
 BANKSEL OPTION_REG
 movwf OPTION_REG
; GOTO LOOP ;<--------------DEBUG
 BANKSEL PORTC
 bcf PORTC,7 ;LCD_RS
 movlw 0x02 ; Still in 8-bit, so appears as 0x20 to LCD
 call w_to_data
 call pulse_e
call delay2
movlw b'00101000'
 call send_w
movlw b'00001110'
 call send_w
movlw b'00000110'
 call send_w
call delay2
BANKSEL PORTC
 bcf PORTC,7 ; Command mode
; movlw b'00000010' ; Return cursor to home
; call send_w
 movlw 0x80
 call send_w
 BANKSEL PORTC
 bsf PORTC,7 ; Data mode
 call delay2 ; Takes a couple of ms
movlw 'H'
 call send_w
 movlw 'e'
 call send_w
 movlw 'l'
 call send_w
 movlw 'l'
 call send_w
 movlw 'o'
 call send_w
 movlw ' '
 call send_w
 movlw 'W'
 call send_w
 movlw 'o'
 call send_w
 movlw 'r'
 call send_w
 movlw 'l'
 call send_w
 movlw 'd'
 call send_w
 movlw '.'
 call send_w
LOOP
 ;
NOP
 BANKSEL PORTC
 BCF PORTC, 5
 call delay2
 BANKSEL PORTC
 BSF PORTC,5
 call delay2
 GOTO LOOP ; loop forever
END

	

DECFSZ [NOT AN] issue on the PIC 16F1509 ?

EDIT: I hate going to the message board… naturally I am licking my wounds from getting scolded for daring to use cblock…

GPR_VAR UDATA

var_name  RES 1

…. took care of this issue… at least that fiasco is over.. and I thought the PWM was going to be the hard part!  /EDIT

This code is killing me. I’ve pulled it out of something I’m working on… it has been kicking my ass for two days now.

The LED on PORTC,2 should be pulses for a few hundred nanoseconds and then go off….. right?  No.. the decfsz sits in an endless loop it seems. Why? I’ve messed up somewhere here?

 

  errorlevel -230, -302, -303, -313
    LIST R=DEC

#include "p16f1509.inc"


    __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _CLKOUTEN_OFF
    __CONFIG _CONFIG2, _LVP_OFF & _STVREN_ON


cblock
    int_delay1
    int_delay2
endc

RES_VECT  CODE    0x0000            ; processor reset vector
    GOTO    START                   ; go to beginning of program


MAIN_PROG CODE                      ; let linker place main program


INIT:
                            ;RC5 = PWM1
    BANKSEL LATA            ;Data Latch
    CLRF LATA               ;
    BANKSEL ANSELC          ;
    CLRF ANSELC             ;Digital IO
    BANKSEL PORTC           ;
    BCF PORTC,5             ;Clear PWM1
    BANKSEL TRISC           ;Set all PORTC to outputs
    CLRF TRISC

    BANKSEL OSCCON
    MOVLW   0x78            ;16MHZ Clock
    MOVWF   OSCCON

    RETURN


START
    CALL    INIT

           BANKSEL int_delay1
            MOVLW   0x01
            MOVWF   int_delay1
            BSF PORTC,2
OLOOP
            NOP
            BANKSEL int_delay1
            DECFSZ int_delay1,1
            GOTO OLOOP
            BCF PORTC,2



    NOP

LOOP:
    NOP
    GOTO LOOP                ;loop forever

    END

PWM on the PIC16F1509 in ASM

[Edit: If you’re looking for NCO (numerically controlled oscillator) usage instead of the PWM module take a look here instead. However note the code is in C (for XC8) not ASM]

Another lazy Sunday… A lot of OT the last few weeks at work and Sunday about the only day I have for relaxation so to clear my mind I wrote some test code for Pulse Width Modulation on a development board I purchased from @TINDE made by @TAUTIC . I’ll attach the code for anyone to use.

MPLAB on the laptop with a @TAUTIC / @TINDE dev board with a PWM test output.
MPLAB on the laptop with a @TAUTIC / @TINDE dev board with a PWM test output.

The code outputs on PWM1 (RC5) at about 1Khz at just under 50% duty cycle but that easy to change if you read the Microchip 16F1508/9 spec sheet section 23.

;*******************************************************************************
;                                                                              *
;    Microchip licenses this software to you solely for use with Microchip     *
;    products. The software is owned by Microchip and/or its licensors, and is *
;    protected under applicable copyright laws.  All rights reserved.          *
;                                                                              *
;    This software and any accompanying information is for suggestion only.    *
;    It shall not be deemed to modify Microchip?s standard warranty for its    *
;    products.  It is your responsibility to ensure that this software meets   *
;    your requirements.                                                        *
;                                                                              *
;    SOFTWARE IS PROVIDED "AS IS".  MICROCHIP AND ITS LICENSORS EXPRESSLY      *
;    DISCLAIM ANY WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED, INCLUDING  *
;    BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS    *
;    FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL          *
;    MICROCHIP OR ITS LICENSORS BE LIABLE FOR ANY INCIDENTAL, SPECIAL,         *
;    INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, HARM TO     *
;    YOUR EQUIPMENT, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR    *
;    SERVICES, ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY   *
;    DEFENSE THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER      *
;    SIMILAR COSTS.                                                            *
;                                                                              *
;    To the fullest extend allowed by law, Microchip and its licensors         *
;    liability shall not exceed the amount of fee, if any, that you have paid  *
;    directly to Microchip to use this software.                               *
;                                                                              *
;    MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF    *
;    THESE TERMS.                                                              *
;                                                                              *
;*******************************************************************************
;                                                                              *
;    Filename:      main.asm                                                   *
;    Date:          Sept 29 2013                                               *
;    File Version:  1.0                                                        *
;    Author:        Charles Douvier                                            *
;    Company:                                                                  *
;    Description:   Test of PWM1                                               *
;
;    Device 16F1509
;
;
;
;
; PIN DIAGRAM
;
;   RA0                             RC0
;   RA1                             RC1
;   RA2                             RC2     STATUS LED
;                                   RC3
;   RA4             RB4             RC4
;   RA5             RB5             RC5     PWM1
;                   RB6             RC6
;                   RB7             RC7
;
;------------------------------------------------------------           *
;*******************************************************************************
;                                                                              *
;    Notes: In the MPLAB X Help, refer to the MPASM Assembler documentation    *
;    for information on assembly instructions.                                 *
;                                                                              *
;*******************************************************************************
;                                                                              *
;    Known Issues: This template is designed for relocatable code.  As such,   *
;    build errors such as "Directive only allowed when generating an object    *
;    file" will result when the 'Build in Absolute Mode' checkbox is selected  *
;    in the project properties.  Designing code in absolute mode is            *
;    antiquated - use relocatable mode.                                        *
;                                                                              *
;*******************************************************************************
;                                                                              *
;    Revision History:
;           2013-09-28  Initial                                                *
;                                                                              *
;*******************************************************************************

;*******************************************************************************
; Processor Inclusion
;
;*******************************************************************************
    errorlevel -230, -302, -303, -313
    LIST R=DEC
#include "p16f1509.inc"
;*******************************************************************************
;
; Word Setup
;
;
;*******************************************************************************
    __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _CLKOUTEN_OFF
    __CONFIG _CONFIG2, _LVP_OFF & _STVREN_ON
;*******************************************************************************
;
;  Variable Definitions
;
;*******************************************************************************
; TODO PLACE VARIABLE DEFINITIONS GO HERE
;*******************************************************************************
; Reset Vector
;*******************************************************************************
RES_VECT  CODE    0x0000            ; processor reset vector
    GOTO    START                   ; go to beginning of program
;*******************************************************************************
; TODO INSERT ISR HERE
;*******************************************************************************
; MAIN PROGRAM
;*******************************************************************************
MAIN_PROG CODE                      ; let linker place main program
INIT:
                            ;RC5 = PWM1
    BANKSEL LATA            ;Data Latch
    CLRF LATA               ;
    BANKSEL ANSELC          ;
    CLRF ANSELC             ;Digital IO
    BANKSEL PORTC           ;
    BCF PORTC,5             ;Clear PWM1
    BANKSEL TRISC           ;Set all PORTC to outputs
    CLRF TRISC
    BANKSEL PORTC
    BSF PORTC,2
    BANKSEL OSCCON
    MOVLW   0x78            ;16MHZ Clock
    MOVWF   OSCCON
    BANKSEL PWM1CON         ;
    CLRF PWM1CON            ;Disable PWM bits
    BANKSEL PR2
    MOVLW 0xFF
    MOVWF PR2               ;Load PR2 with 0xFF
                            ;Timer/PR set up is 973Hz
    BANKSEL PWM1DCH
    CLRF PWM1DCH
    BANKSEL PWM1DCL
    CLRF PWM1DCL
    BSF PORTC,2
    BANKSEL PWM1DCH
    MOVLW   0x6F
    MOVWF   PWM1DCH

                            ;copied code, havent check this yet.
    ;ENABLE INTERRUPT
    BANKSEL PIE1
    BSF PIE1,1
    bcf        PIR1,2
                    ;-----------------------------------
                    ;Configure and start Timer2
;CONFIGURE TIMER.. copied code review.
    BANKSEL T2CON
    MOVLW B'00000110'   ;MOVLW B'00000110'
    MOVWF T2CON
                    ;Enable PWM output pin and wait until Timer2
                    ;overflows, TMR2IF bit of the PIR1 register is set.
                    ;See note below.
    btfss    PIR1,1                ;Test for T2 interrupt
    goto    $-1
                    ;Enable the PWMx pin output driver(s) by clearing
                    ;the associated TRIS bit(s) and setting the
                    ;PWMxOE bit of the PWMxCON register.
                    ;Configure the PWM module by loading the
                    ;PWMxCON register with the appropriate values.
    BANKSEL TRISC          ;Enable Outputs
    CLRF    TRISC
    BANKSEL PWM1CON        ;Enable PWM1
    MOVLW    B'11000000'
    MOVWF    PWM1CON
    RETURN
START
    CALL INIT
    BANKSEL PORTC            ;select PORTC for heatbeat
LOOP:
    BSF PORTC,2              ;heart beat
    MOVLW 0x55
    NOP                      ;future code
    BCF PORTC,2
    GOTO LOOP                ;loop forever
    END