Wednesday 18 January 2012

Timer Interruptus...

Slowly but steady goes the progress.I have now added Timer 2 interrupt handler and real time (1ms accurate) clock to my code. With this timer system I don't need to use busy looping for time keeping and I can later adjust the system for other real.time needs.

There are two important key parts to consider at this version of program. The interrupt service routine and the Timer2 initialization routine.

The interrupt service routine itself consists of three parts. At beginning, it saves the W and STATUS registers so that those can be used at interrupt without distrupting the main program. I have selected unbanked RAM (at 0x70-0x7f) for storage cause those ram locations are always available. Also it's essential that these same registers are restored before returning from the interrupt handler.

Middle part of the routine looks quite a simple for now. There is just increment of the multibyte millisecond counter and reset of timer interrupt flag. This code will grow at the future depending what applications I'll make with this micro controller.

Next likely parts will be implementing interrupt based software serial port, transmitting for debug purposes. Most likely I'll use 9600bps and try to keep the code minimal. After that the next possible direction is to make test routines for the A/D converters.

After those.. I hope I'll have all the parts for an application I've been wanting to make for some time now, the parts are in Slow-mail coming from another side of the earth so there is no  telling when they manage to appear.

--- Cut here.. Code follows

; minimal -led blinker
; V2 with Timer2 interrupt

; Uses Timer2 to generate 1ms timer interrupt

;PortC pins:
; RC:0 = digital out, led active high

  #include p16f616.inc 
  __config _INTOSCIO & _WDT_OFF  ;   8 Mhz int-osc with IO:s
      

 ; define Data memory
inner equ 0x20
outer equ 0x21
 
 ; Bank shared memory (0x70-0x7f) used for Interrupt
 ; Interrupt backup registers
int_w          equ 0x70
int_status     equ 0x71
int_pclath     equ 0x72 ; not yet used at interrupts
int_indf    equ 0x73 ; not yet used at interrupts

 ; Debug serial port (Note.. buffer location not yet determined
ser_tx        equ 0x74  ; Zeroes reg after send
ser_rx        equ 0x75  ; 0 = no data
ser_status    equ 0x76  ; Todo! ; Define modes/error codes


 ; 24-bit ms tick counter (allocation for 32-bit int)
ms_32     equ 0x7c
ms_high equ 0x7d
ms_med  equ 0x7e
ms_low  equ 0x7f
               
  ; Start from reset vector
  
  org 0x00 ; obvious, but just in case.
  goto Main ; Skip over interrupt vector

  org 0x04 ; Interrupt vector
Intserver
  ; Save W, STATUS  (3 cycles)
  movwf int_w
  swapf STATUS,W ; only way to move status without affecting the Z-flag
  movwf int_status

  ; Real interrupt routine (remember to clear the interrupt)
  ; note: We have no idea about status flags (including RP0)
  ; Since this interrupt happens every 2000:th cpu clock, we'll just
  ; increment the ms-counters 
  movlw 0x01
  addwf ms_low, F
  btfsc STATUS, C ; if carry not happen skip increment
   addwf ms_med, F
  btfsc STATUS, C ; if carry not happen skip increment
   addwf ms_high, F
  bcf PIR1, TMR2IF ; Clear interrupt 


Intserver_end
  ; Restore W, STATUS and return from int (6 cycles)
  swapf int_status, W
  movwf    STATUS 
  swapf int_w, F
  swapf int_w, W  ; return W without messing with Status flags   
  retfie ; return with interrupts enabled.

  org 0x100  ; main program
Main
 ; Init IO ports
  BCF STATUS,RP0 ;Bank 0
  CLRF PORTC ;Init PORTC
  BSF STATUS,RP0 ;Bank 1
  CLRF ANSEL ;digital I/O  (disable Analog Inputs)
  MOVLW 0xFE ;Set RC<5:1> as input
  MOVWF TRISC ;and set RC<0> as outputs
  BCF STATUS,RP0 ;Bank 0

 ; Init Timer2 and real time clocks
  movlw 0x00
  movwf ms_high
  movwf ms_med
  movwf ms_low
 
 ; Sample of Timer2 settings for various interrupts at 8MHz xtal.
 ; Values in decimal.
 ; Clk    PR2    Pre    post    Usage
 ; 2000    125    16    1        1ms timer for real time system clock
 ;  208    208     1     1         9600 bps Tx async debug serial
 ;   69  69     1    1        9600 bps Rx async debug serial (3* subsample)
 ; Last one will tax system badly

 clrf TMR2
 bcf PIR1, TMR2IF ; Clear interrupt (should be anyhow)

 bsf STATUS, RP0 ; Bank1
 movlw 0x7d ; 125 decmal
 movwf PR2
 bsf PIE1, TMR2IE ; Allow timer2 interrupt.
 
 bcf STATUS, RP0 ; bank0
 movlw 0x06 ; Prescaler *16 + OscOn. 
 movwf T2CON ; Set t2 scalers + start the timer  
 bsf INTCON, PEIE ; Enable periferal int
 bsf INTCON, GIE ; Global interrupt enable
 

Loop
  movf  ms_med,W 
  movwf PORTC    ; Blink period of 512 ms

  goto  Loop             ;Do this repeatedly

  end                    ;All things must end someday

Saturday 14 January 2012

Microcontroller Ahoy

And here we are. No more talking about 3D printer or parts I have designed and/or made (*), now it's time for some microcontroller projects.

Couple days ago I ordered basic programming tool and ten Pic 16F616 controllers. Pictured here is the first test circuit running short program I made earlier today. Here Pickit2 is used as programming device and also it provided the power for running the test circuit. LED with current limiting resistor is connected from RC0 (Pin10) to  ground, and the one small capacitor is located between 16F616:s ground (Pin 14) and operating voltage (Pin1) for filtering the power fluctuations at the circuit.

Prototype board with Pic 16F616 connected to Pickit2 programmer
This is not the first time I have been using Microchip Pic chips, since my first Pic based projects were made with classic 16C84:s like fifteen years ago. This 16F616 has a lot improvements compared to that older chip. Having multiple timers and option to use Timer1 with 32Khz quarts to build real time clock will be handy. Also the build in 10-bit A/D converter will have it's uses as will the extended PWM -modes available. 

Making assembler code for these cheap-class  (about one Euro, in small numbers) controllers is a bit like driving a bike, you have learned it once and you'll have the skill rest of your life. Of course it can take few hours to get all necessary manuals and data sheets, and some time will be spend on software and tools playing nicely together.



My bit kludgish  asm-source follows, in case anyone is interested. :-)

; Not so nice example.
; PortC pins:
; RC:0 = digital out, led active high

  #include p16f616.inc 
  __config _INTOSCIO & _WDT_OFF  ;   8 Mhz int-osc with IO:s
                      
  ; Start from reset vector 
  org 0x00 ; obvious, but just in case.
  goto Main ; Skip over interrupt vector

  org 0x20  ; main program
Main
  BCF STATUS,RP0 ;Bank 0
  CLRF PORTC ;Init PORTC
  BSF STATUS,RP0 ;Bank 1
  CLRF ANSEL ;digital I/O  (disable Analog Inputs)
  MOVLW 0xFE ;Set RC<5:1> as input
  MOVWF TRISC ;and set RC<0> as outputs
  BCF STATUS,RP0 ;Bank 0

Loop
  incf  PORTC,F   ;Read PORTC, add 1 and save back
  goto  Loop      ;Like forever


  end             ;All things must end someday



(*) PS: As a footnote I'll want to show you one thingy I designed couple days ago. It's a kitchen towel hook, made to fit at two centimeter thick cabinet doors. Drawings and prints are available at request.




Towlie, a homemade kitchen towel holder.

This is something I was looking for some time ago and could not find one from any shops. A simple hook that will hold the kitchen towel right where I need it.

I used to keep the towel folded atop of the cupboard door, but it tends to slide of and drop to floor. With this simple hook, the towel won't fall, but it's easy to access. And when printed with clear PLA it next to invisible.

As usual, 3D design is made with Sketchup program and then converted to STL with exporter-plugin.

Apparently I entirely forgot to make post of my Towlie-design. So I will backdate this to around the time I uploaded the files to Thingiverse. I originally designed this thingy at 10th Jan 2012 but uploaded it at 14th.

Original design files available, with free CC lisense. Just download and print as many as you need. I'd like to hear your feedback of this design, does it work or not, does it need any modifications or is it good just as it is?

Saturday 7 January 2012

New year, old stuff, aka: Short intro to Polymorph

With all interest to 3d printing I've been neglecting some other ideas way too long. So now is high time to start introducing some older stuff. And I think Polymorph is a good start.

Polymorhph, as sold, white granules
Polymorph: Also known and sold with names like Friendly Plastic, is a relatively new plastic polymer that softens at the hot water. At room temperature it's while and feels a lot like Nylon, a bit flexible and not at all brittle.

When heated to 70 Celsius (or so) it becomes clear, a bit sticky putty that can easily be shaped by hand. There are many ways you could do the heating but I prefer pouring some freshly boiled water atop and let it melt for a while.

Home made phone-tripod adapter
One of the first things I did with Polymorph was a tripod adapter for my smartphone. For some reason, even though most of today's phones have video recording capabilities, there seems to be no way to connect these to tripods.

Making my own  tripod adapter was quite simple: Firstly I covered relevant part of my phone with clear packing tape, so I can be sure the melted pastic don't take too much hold.

Attached to desktop tripod
Then I took the heated, flexible polymorph, removed the excess water of it and started to shape it around my phone while making sure I don't cover anything important, like touch screen, connectors or buttons. 

As soon as polymorph was at the desirable shape I pressed it (along my phone) atop of my tripod head, so that plastic got around the 1/4" bolt that's used for fixing the camera, and I let the plastic cool back to room temperature.


After some wait, I then removed camera from the now white solid part and removed the protection tape of the phone.

The adapter works well, and keeps phone steady for making videos. At the same time: This adapter was/is a quick hack, so there's definitely not much hope winning any industrial design awards with it. ;-)

All in all, I have used Polymorph quite few projects so far and I like the speed I can use it to make the unique objects I need. It's also good for making low temperature molds of existing objects and repairing things.