Tuesday, 28 August 2012

Timers ( AVR ) : The theory behind it

*As the Analog to Digital tutorial is going take longer than i estimated at first, i'll leave you another introductory on Timers...   

While the use of delay() can be useful for simple tasks,  will also keep the uC busy until it it finishes waiting ! 
So, we are going to have a look at the timers of an AVR chip !

    A timer is a simple counter, controlled by registers. Its advantage is that the input clock and operation of the timer is independent of the program execution. This makes it possible to measure time by counting the elapsed cycles and take the input frequency of the timer into account. So instead of providing instructions in the program that are executed regularly and increment a register, the microcontroller does this all by itself. It is possible to connect the timer to the system clock and thus take advantage of the accuracy of the crystal.
    The AVR has an interesting counting circuit. This circuit has 2 inputs and one output. If you tie it into the Tn pin it will count the pulses on the pin (this is referred to as a counter on the datasheet). If you tie it into the AVRs internal(or external) clock you now have a timer. If you tie the counter to the output you will get an Pulse Width Modulator signal on the OCnx pin. 

    When the Control Logic gets a pulse from the pre-scaler (more on that later) it increments/decrements the TCNTn register. The TCNTn register is compared to the OCRn register. When the TCNTn register reaches the same value as the OCRn value or the TOP/BOTTOM value (0xFF for 8bit Registers/Counters and 0xFFFF for 16bit  Registers/Counters ) the Control Logic clears the TCNT register and activates the TOVn bit (Timer Overflow) which stays set until it is reset by the user. Figure below shows this

- 1stt ( red) shows the pulses generated by the system clock(clk).
- 2nd ( blue) shows the pulses coming out of the pre-scaler (clk_tn or clk/8). In our example the pre-scaler is set to 8 so, it divides the clk(red) by 8.
- 3rd shows the TCNTn (basically the count). The TCNTn updates on the falling edge of the pulse coming out of the pre-scaler. On our first pre-scaler pulse ( blue) our TCNTn is TOP-1 (or 254). On our 2nd pre-scaler pulse( blue) our TCNTn goes to TOP (or 255). Now, on our third pre-scaler ( blue) pulse the TCNTn register overflows back to the BOTTOM (or 0) because it was at its TOP value on the previous pulse. On our 4th pre-scaler pulse ( blue) the TCNTn register gets incremented again this time to BOTTOM + 1 (or 1).
- The 4th shows the TCNTn register again. The only difference is that it shows TCNTn register in a Phase Corrected PWM pattern, which means that it counts up until it hits the TOP and then start to count down until it hits the BOTTOM . * More about this in the PWM tutorial.
- The 5th line ( green) shows us the TOVn (Timer Overflow bit). This bit is set the first time that a timer overflows. Or in other words the first time that the timer goes from its TOP state to its next state (BOTTOM in the 3rd line or, TOP -1 in the 4th line).
- The 6th line shows the OCRnx register. This register deals with the PWM. The only thing that it is showing, is that the OCRnx register can only be updated when an TOVn (timer overflow) condition happens. * More on the OCRnx register in the PWM section.1


In other words, the pre-scaler counts the number of input pulses (from the internal clock or from external sources) and when the number reaches the preset number (0, 8, 32, 64, 128, 256 or 1024) it generates a pulse of its own. This can be seen in Figure 2 (above) which divides the input by 8. This is needed because the AVR only has an 8bit and a 16bit register

Now the maths :

The math is fairly simple:

Pre-scaler = Pre-scaler / Pre-scaler


Pre-scaler = Pre-scaler * Pre-scaler

And don't forget the old

Frequency = Period / Time

Then, to calculate how long would take to the AVR to generate a pulse ?!?
Frequency = Period / Time

Period / Frequency = Time

Time = Period / Frequency

Time = 16bit_register_size / System_clock_speed

Time = 0xFFFF(aka 65535 )/ 16Mhz


What if we use a pre-scaler of (lets say ) 1024 ?!

 = Pre-scaler * 

Pre-scaler =   Time   * 1024


In the datasheet says that it is recommended that all external sources are limited to system clock / 2.5 . The Arduino runs at 16Mhz therefore  you are only able to reliably measure up to an  6.4Mhz  signal or less.
   Also, starting the pre-scaler will start the timer, so I recommend setting the pre-scaler bits last when setting up your timer in your program.

Now, i will leave you with a handy reference table, that i am sure will be helpful many a time !!

As a taster, ill leave you a simple code here for a blinky, to use on the Arduino Board ( as Port B  5/ pin 13 is used for led ); I included a commented pre-scaler at 1 just for reference

#include <avr/io.h> 

int main (void) 
  unsigned char ElapsedTime = 0; // Make a new counter variable and initialise to zero 

  DDRB |= (1 << 5); // Set LED as output 
  TCCR1B |= (1 << CS10); // Set up timer at Fcpu/1 

  for (;;) 
    // Check timer value in if statement, true when count matches 1/2 second 
    if (TCNT1 >= 16000) 
      TCNT1 = 0; // Reset timer value 

      if (ElapsedTime == 244) // Check 
        ElapsedTime = 0; // Reset counter variable 

        PORTB ^= (1 << 5); // Toggle the LED 

Reference :
- AVR130: Setup and Use the AVR Timers

- Reference table
- Timers and counter

Saturday, 25 August 2012

Beyond the GPIO Pins : Peripherals & Intro to ADC

While in the Arduino, a lot of the ins and outs of the real functioning of the chips has been made easier, when we start making the change into AVR C coding, we will have to start understanding the real thing that is an ATMega chip !

One of the things is start understanding all the peripherals that the chip has, which means going beyong the ON-OFF digital function of a pin.

That can include( depending on the chip): Digital I/O -- On or Off; Parallel signals; Pulse-width-modulated logic signals; Counters and timers

Analog I/O -- Voltages; Comparators; Analog-to-digital converters (ADCs); Digital-to-analog converters (DACs).

But why do we need ADC's ?!? Well, a lot of sensors that are used in Electronics(and Robotics) give output voltages in the form of potential differences in voltages; We can attach an interrupt to the conversion, so that we dont need to constantly keep checking on it; We can also use it with potentiometers to control a variable depending on the value's reading, and many other applications im sure you will think of !

So lets start with the Analogue to Digital converters that the ATMega has . As far as Analogue to Digital Inputs go, it states we have available 6 channels with a 10 bit resolution ! This means that you can distinguish between two readings which have a difference of Reference Value/210 . The max value is broken up into 210-1 = 1023 parts and if you have a reference of say 5V, you can distinguish between readings 4.88mV apart.

The ADC is a 10-bit ADC, ie, it provides measurements in the range 0 to 1023. Situated at the entrance of the input is the Ground , so the ADC provides a value of 0.If the voltage at the input matches the reference voltage ( not quite), then it passes a value of 1023. If the 10-bit resolution is not needed, it is possible to restrict the output by a configuration register so that a easy access to the 8 most significant bits is possible.

As with many analog circuits, the ADC is also subject to noise . This means that one should not assume that the ADC input at a constant voltage outputs always the same constant value. A "trembling" of the least significant 2 bits is not unusual. Should be emphasized again at this point, the quality of the reference voltage. This quality will significantly change the quality of the results. The circuit of AREF with a suppressor filter capacitor is the absolutely necessary to obtain a reasonably acceptable reference voltage.If this is not sufficient, then by oversampling you can increase the quality of measurement. For this purpose, several measurements are made to form an average.

The Atmega168 uses 6 different registers when performing analogue to digital conversion.
  • ADMUX   (ADC Multiplexer Selection Register), 
  • ADCSRA  (ADC Control and Status Register A), 
  • ADCSRB  (ADC Control and Status Register B), 
  • DIDR0      (Digital Input Disable Register 0), 
  • ADCL       (ADC Data Register – Low) 

  • ADCH (ADC Data Register – High).
The ADMUX register allows you to control reference Voltage, left adjustment of results (used for 8 bit results) and selection of input channel.

Reference voltage is controlled by REFS1 and REFS0. AREF is used by default, but there are also other options available (more about it soon) ! (see graphic)

ADLAR is used for left-shifting the converted data, useful when reading 8 bit values .(see graphic)
0 -The result is right-aligned in the registers ADCH / ADCL. The 8 least significant bits of the result are stored in ADCL. The remaining 2 bits of the result are stored in register ADCH in bits 0 and 1.
1 - The result is left-aligned in the registers ADCH / ADCL. The 8 most significant bits (MSB) of the result are stored in ADCH. The remaining 2 low order bits (LSB) are stored in the register ADCL in bits 6 and 7.

ADC Data Register

If conversion is finished, the measured value is in these two registers. Of ADCH only the two least significant bits are used. It must always be read both registers, and always in the order: ADCL, ADCH . The actual measured value is then given by:

x = ADCL; / / with uint16_t x 
= x + ( ADCH << 8 ) ; / /(LSB / MSB order, and  in two lines to ensure / / C operator precedence)


x = ADCW; / / depending on the AVR and x = ADC (see avr / ioxxx.h)

MUX0 to MUX3 are used to select which input channel you wish to read. (The values 0000 to 0101 allow you to select ports PC0 to PC5, while the reserved values of 1110 and 1111 allow you to select 1.1V and 0V.

ADCSRA and ADCSRB are used to control the Analogue to Digital conversion .

    bit               7            6              5          4        3          2             1              0
ADCSRB      -          ACME            -          -         -       ADTS2   ADTS1     ADTS0
Read/Write    R        R/W            R          R        R        R/W       R/W         R/W

Initial Value    0            0             0           0         0         0            0              0

"ADC Enable": ADEN means the ADC is switched on and off. A 1 in that bit position turns on the ADC.
"ADC Start Conversion": If a 1 is written to this bit position, the ADC starts the conversion. The bit remains set to 1, so long as the conversion is in progress. When the conversion is complete, this bit of the ADC is reset to 0.
"ADC Free Running": If a 1 is written to ADFR, the ADC operates in Free Running mode. Here, the ADC starts after the completion of a measure automatically the next measurement. The first measurement is normally by setting the ADSC bit started.
"ADC Interrupt Flag": If a measurement is completed, the ADIF bit is set. If additionally the ADIE bit is set, then an interrupt is triggered and the corresponding interrupt handler called.
" ADC Interrupt Enable": If a 1 is written to ADIE, the ADC triggers a measurement after an interrupt.
"ADC Prescaler": The ADCprescaler can be selected into a  frequency. According to the data should be between 50kHz and 200kHz as the resolution optimal for this . ( some quirks here, but we will see more about it ahead)

This graphic gives you get a better birds eye view and and should help understand better the whole process (  Courtesy of  web.alfredstate.edu )


* to be Continued...

Wednesday, 22 August 2012

Tarte de Framboesas aka Raspberry Pi

Last delivery brought Raspberry Pi and a PIC16F887 Dip.
For those willing to use a translator on their browser, heres a portuguese blog on the RP board !! 

Also make sure you check wiringPi language, for the Raspberry Pi.
Its mentor says
"You may be familiar with the Arduino system… Briefly; Arduino is really two things; one is a hardware platform, the other software, and part of the software is a package called Wiring. Wiring is the core of the input and output for the Arduino, so I thought it would be good to replicate that functionality (or a good usable subset) on the Raspberry Pi.
WiringPi is a Wiring library written in C and should be usable from C++ and many other languages with suitable wrappers ." ( Link )

From the General Purpose Input/Output (GPIO) connector we can get :
- 8 general purpose digital I/O pins ( which can be programmed as either digital outputs or inputs).
- One pin (Pin1) can be designated for PWM output .
- 2-wire I2C interface &
- 4-wire SPI interface (with a 2nd select line, 5 pins in total) 
- Serial UART ( further 2 pins )

* I2C and SPI interfaces can also be used a general purpose I/O pins when not being used in their bus modes, and the UART pins can also be used if you reboot with the serial console disabled, giving a grand total of 8 + 2 + 5 + 2 = 17 I/O pins.

Additional functions

- shiftIn/shiftOut library
- Serial port handling (both on-board and USB)
- LCD library
- Simplified threaded programming and process priority control

Project code

Monday, 6 August 2012

3 nice books in the charity shop !!

3 nice books found in the charity shop !! 
Specially the Data Converters one, by G.B.Clayton (RS technical library marker lol).

Friday, 3 August 2012

Assembler language endeavours in PIC land ...

I find the architecture of the PIC harder to grasp...
On studying Assembler language for Microchip PIC uC's managed to get confused with the real role of the STATUS Register ( RP0 and RP1 ). And why you have to resume it back to bank 0, after each of the changes...

Code is this :

#include <p16F887.inc>

     org 0
     bsf     STATUS,RP0  ; select Register Bank 1
     bcf     TRISD,0     ; make IO Pin RD0 an output
     bcf     STATUS,RP0  ; back to Register Bank 0
     bsf     PORTD,0     ; turn on LED RD0 (DS0) 
     bcf  PORTD,0
     bcf     STATUS,RP0
  bsf     STATUS,RP0  ; select Register Bank 1
     bcf     TRISD,1     ; make IO Pin RD1 an output
     bcf     STATUS,RP0  ; back to Register Bank 0
     bsf     PORTD,1     ; turn on LED RD1 
     bcf  PORTD,1
     bcf     STATUS,RP0
  bsf     STATUS,RP0  ; select Register Bank 1
     bcf     TRISD,2    ; make IO Pin RD2 an output
     bcf     STATUS,RP0  ; back to Register Bank 0
     bsf     PORTD,2     ; turn on LED RD2 
     bcf  PORTD,2
     bcf     STATUS,RP0
         bsf     STATUS,RP0  ; select Register Bank 1
     bcf     TRISD,3    ; make IO Pin RD3 an output
     bcf     STATUS,RP0  ; back to Register Bank 0
     bsf     PORTD,3     ; turn on LED RD3 
     bcf  PORTD,3
     bcf     STATUS,RP0
         bsf     STATUS,RP0  ; select Register Bank 1
     bcf     TRISD,4    ; make IO Pin RD4 an output
     bcf     STATUS,RP0  ; back to Register Bank 0
     bsf     PORTD,4     ; turn on LED RD4  
     bcf  PORTD,4
     bcf     STATUS,RP0
  goto    Start      ; wait here

So it seems that this is the way banks work: In the status SFR, bit 5 , bit 6 and bit 7 are RP0, RP1 and IRP ( which It is used for indirect addressing), So The two banks used are RP0 and RP1. And they have.the RAM divided in 4 each of RP0 and RP1, which are blocks and we need to change it in order to use them ( im told this is one of the points in favour of the ATMEL chips). If we after using arrays then we'd have to use FSR-INDF ( not relevant now).

I was then also told, that MPLAB has some macros that sort it for you.
Its name is banksel .
So, after some research here is my way of seeing it...
The banksel will sort out for us the stuff related to the status for us, seting the bits and clearing them for us.

#include <p16F887.inc>

     org 0

  banksel TRISD           ;Include standard header file
                          ;for the selected device...
  clrf    TRISD           ;Clear TRISD. Sets PORTD to outputs.
  banksel PORTD           ;banksel used to return to bank 0,
                          ;where PORTD is located.

  movlw   0x55            ;Set PORTD value.

  movwf   PORTD
  clrf    PORTD
  goto    Start
  end                     ;All programs must have an end.

Also, managed to get the PCLATCH register understood, today ( Ill just say that is the counter of the program, and will cover the number of up to 8192, divided by the 13 bits of the SFR's PCL and PCLATCH (2^13=8192) , always incrementing automatically, though having the Least significant bits( LSB) on the PCL... there seem to be some quirks here for some cases, but im told to leave that for now !!

For completions of info:
PIC16F887 44 Pins, 8 leds on PORTD and PickIt2 debugger !
PS- Another code, brought from someone @ http://lusorobotica.com, explaining that you can set all your bits first, and then bring back the register back to bank 0.

#include <p16F887.inc>

org 0x00


     bsf     STATUS,RP0  ; choose Register 1
     MOVLW 0x00          ;load register W with 0
     MOVWF TRISD         ;Start register TRISD with 0 (Moved from the already loaded value in W)
     bcf     STATUS,RP0  ; Bit clear, back to Bank 0
     MOVLW b'10101010'   ; A pattern to light the LEDs (bynary, with 1 for on and 0 for off)
     MOVWF PORTD        ;write to port D
     GOTO $              ; 

     end                ;The End !

Some people who been totally to blame for these adventures in PICland... and one is definitely SENA, who you can visit here http://www.antoniosergiosena.com/ .
He has a wicked tutorial in Portuguese which been priceless to me ! With examples in both C and ASSEMBLER; I chose Assembler first  ( i know, i know...)
Man is a PRO in PIC assembler and C. Hands down !!
Also, as mentioned above, http://lusorobotica.com.

Some reference