Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 10, 2016, 09:17:40 21:17


Login with username, password and session length


Pages: [1]
Print
Author Topic: PIC ADC Oversampling  (Read 3271 times)
0 Members and 1 Guest are viewing this topic.
thunderer
Junior Member
**
Offline Offline

Posts: 54

Thank You
-Given: 18
-Receive: 61


I try to be patient


« on: July 19, 2014, 04:16:40 04:16 »

I am not bringing up something new, but I felt the forum needed an ADC Oversampling topic. I looked for it, and found no such example.

It is based on Florin (YO2LIO) post on Mikroelektronika forum.

This is a complete application of the function Florin put there. There are 3 AN I/O (AN0, 1 and 2) used (originally were 4 but I cut to 3) and I print on a LCD the values of 10bit, 12bit and 14bit. The fourth one (the 16bit) is very slow, but you can implemented if you do not need speed.

The PIC is an 16F690 at 8MHz (internal osc), ref is Vdd (mine was 4.99V). The code is below. Comments are made when I considered needed (sometimes I forget why I put some code line  Tongue). All readings are scaled to 50000 (this explains the multipliers I used for the 10bit value, etc...). All the 3 ADC are in parallel.

Notes: if your voltage (the one you want to measure) is very clean you'll see no diference in displayed values, if it is slightly varying, you will see how the 10bit varies by 50mV, 12bit by 12mV, and 14bit by 3mV before stabilizing to the same value.

On compile: free RAM 80%, free ROM 48%.

Code:
' *
' * Project name:
'     ADC Oversampling (up to 16 bit)
' * Copyright:
'     (c) Thunderer (07/2014)
' * Revision History:
'     0 - 1st issue
' * Description:
'     See Mikrobasic forum: Florin YO2LIO post (Mikrobasic and Mikropascal)
' * Test configuration:
'     MCU:             PIC16F690
'     Oscillator:      INTOSC 8 MHz
'     Ext. Modules:    -
' *

program _16F690_ADC_Oversampling
' Declarations section
dim adc_rd0, adc_rd1,adc_rd2, adc_rd3     as longword
dim adc_res1, adc_res2, adc_res3          as longword
dim ch0, ch1, ch2, ch3                    as byte

' Lcd module connections
dim LCD_RS as sbit at RC5_bit
    LCD_EN as sbit at RC4_bit
    LCD_D4 as sbit at RC3_bit
    LCD_D5 as sbit at RC2_bit
    LCD_D6 as sbit at RC1_bit
    LCD_D7 as sbit at RC0_bit

    LCD_RS_Direction as sbit at TRISC5_bit
    LCD_EN_Direction as sbit at TRISC4_bit
    LCD_D4_Direction as sbit at TRISC3_bit
    LCD_D5_Direction as sbit at TRISC2_bit
    LCD_D6_Direction as sbit at TRISC1_bit
    LCD_D7_Direction as sbit at TRISC0_bit
' End Lcd module connections

'   Set-up the PIC
sub procedure InitMain()
    OPTION_REG =   0x80              ' Pull-up disabled PORTB
    INTCON     =   0x00              ' No INT
    
    CM1CON0    =   0x00              ' No COMP
    CM2CON0    =   0x00              ' No COMP
    CM2CON1    =   0x00              ' No COMP
    
    ANSEL      =   0x0F              ' PORTA Analog I/Os on AN0, 1, 2 and 3
    ANSELH     =   0x00              ' No Analog I/Os

    TRISA      =   0x17              ' Inputs RA0, 1, 2 and 4
    TRISB      =   0x00              ' All outputs
    TRISC      =   0x00              ' All outputs
    
    
    PORTA      =   0x00              ' Clear PORTA
    PORTB      =   0x00              ' Clear PORTB
    PORTC      =   0x00              ' Clear PORTC
end sub

sub function Adc_Read_Ext_Res(dim resolution, ch_ as byte) as longword
dim i as longword
    j as byte

  result = 0
  if resolution < 11 then exit end if ' minimum resolution is 11 bits
  resolution = resolution - 10
  j = resolution - 1
  i = 4
  while j > 0
    i = i << 2
    dec(j)
  wend
  while i > 0
    result = result + Adc_Read(ch_)
    dec(i)
  wend
  result = result >> resolution
end sub

'   Main program
Main:
  InitMain()
  Lcd_Init()                     ' Initialize Lcd
  Lcd_Cmd(_LCD_CLEAR)            ' Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF)       ' Cursor off

  ADC_Init()                     ' Initialize ADC module

' Display static text
  Lcd_Out(1,1," 0:")
  Lcd_Out(1,9," 2:")
  Lcd_Out(2,1," 4:")
'  Lcd_Out(2,9," 6:")


' The measurement
Measure:
  while (TRUE)
' Voltage ADC_10b
ADC0:
    adc_rd0 = ADC_read(0) * 48.82813
    ch0 = adc_rd0 div 10000
    Lcd_Chr(1, 4, 48+ch0)
    ch0 = (adc_rd0 div 1000) mod 10
    Lcd_Chr(1, 5, 48+ch0)
    ch0 = (adc_rd0 div 100) mod 10
    Lcd_Chr(1, 6, 48+ch0)
    ch0 = (adc_rd0 div 10) mod 10
    Lcd_Chr(1, 7, 48+ch0)
    ch0 = (adc_rd0 div 1) mod 10
    Lcd_Chr(1, 8, 48+ch0)
    Delay_ms(100)

' Voltage ADC_12b
ADC1:
    adc_res1 = Adc_Read_Ext_Res(12,1)  ' 12 bit resolution, channel 1
    adc_rd1 = adc_res1 * 12.20703
    ch1 = adc_rd1 div 10000
    Lcd_Chr(1, 12, 48+ch1)
    ch1 = (adc_rd1 div 1000) mod 10
    Lcd_Chr(1, 13, 48+ch1)
    ch1 = (adc_rd1 div 100) mod 10
    Lcd_Chr(1, 14, 48+ch1)
    ch1 = (adc_rd1 div 10) mod 10
    Lcd_Chr(1, 15, 48+ch1)
    ch1 = (adc_rd1 div 1) mod 10
    Lcd_Chr(1, 16, 48+ch1)

' Voltage ADC_14b
ADC2:
    adc_res2 = Adc_Read_Ext_Res(14,2)  ' 14 bit resolution, channel 2
    adc_rd2 = adc_res2 * 3.05176
    ch2 = adc_rd1 div 10000
    Lcd_Chr(2, 4, 48+ch2)
    ch2 = (adc_rd2 div 1000) mod 10
    Lcd_Chr(2, 5, 48+ch2)
    ch2 = (adc_rd2 div 100) mod 10
    Lcd_Chr(2, 6, 48+ch2)
    ch2 = (adc_rd2 div 10) mod 10
    Lcd_Chr(2, 7, 48+ch2)
    ch2 = (adc_rd2 div 1) mod 10
    Lcd_Chr(2, 8, 48+ch2)

'' Voltage ADC_16b
'ADC3:
'    adc_res3 = Adc_Read_Ext_Res(16,3)  ' 16 bit resolution, channel 3
'    adc_rd3 = adc_res3 * 0.76294
'    ch3 = adc_rd3 div 10000
'    Lcd_Chr(2, 12, 48+ch3)
'    ch3 = (adc_rd3 div 1000) mod 10
'    Lcd_Chr(2, 13, 48+ch3)
'    ch3 = (adc_rd3 div 100) mod 10
'    Lcd_Chr(2, 14, 48+ch3)
'    ch3 = (adc_rd3 div 10) mod 10
'    Lcd_Chr(2, 15, 48+ch3)
'    ch3 = (adc_rd3 div 1) mod 10
'    Lcd_Chr(2, 16, 48+ch3)

  wend
end.
« Last Edit: July 19, 2014, 04:18:45 04:18 by thunderer » Logged

Interested and hopefully helpful in: DC brushed motor control (mainly R/C - PPM/PWM), analog audio, PIC (mikrobasic PRO). Feel free to ask, and if I can, I will help. But only on forum topics, any started private conversation will continue in a public topic.
Checksum8
V.I.P
Junior Member
*****
Offline Offline

Posts: 71

Thank You
-Given: 45
-Receive: 53


« Reply #1 on: July 19, 2014, 05:06:18 17:06 »

Here is another example if you program with pic basic pro. Written by Darrel Taylor, who unfortunately passed away recently.

http://www.darreltaylor.com/DT_Analog/
Logged
CocaCola
V.I.P
Senior Member
*****
Online Online

Posts: 398

Thank You
-Given: 108
-Receive: 182


« Reply #2 on: July 19, 2014, 06:10:45 18:10 »

Written by Darrel Taylor, who unfortunately passed away recently.

Wow, missed that news, he shared SO much with the PBP community, he will surely be missed!
Logged
bbarney
Moderator
Hero Member
*****
Offline Offline

Posts: 2403

Thank You
-Given: 405
-Receive: 544


Uhm? where did pickit put my mute button


« Reply #3 on: July 19, 2014, 08:36:19 20:36 »

Wow, missed that news, he shared SO much with the PBP community, he will surely be missed!
wow I missed that one too

Quote
The passing of Darrel Taylor

While in his home on the morning of Tuesday, June 24, Darrel suffered a massive heart attack and died. He was 54 years old.

 Darrel was an integral part of the melabs team. His immeasurable expertise touched the lives of countless people through the years.
Logged

Ever wonder why Kamikaze pilot's wore helmet's ?
Cain
Junior Member
**
Offline Offline

Posts: 90

Thank You
-Given: 102
-Receive: 106


« Reply #4 on: August 14, 2014, 03:10:13 15:10 »

Written by Darrel Taylor, who unfortunately passed away recently.

Ohh really sorry to hear that - condolences to his family! Learned alot from his code...

Logged
Vineyards
Active Member
***
Offline Offline

Posts: 130

Thank You
-Given: 36
-Receive: 25


« Reply #5 on: August 15, 2014, 09:15:06 09:15 »

I know quite a few knowledge hungry scientist who after climbing the academic ladder fast reaching a level of perfection died unexpectedly at a very young age. Perhaps so many sleepless nights and a chronic neglect of bodily health are the reasons to blame. Some of us might be going through a similar experience. So beware. In any case, RIP Darrel.
Logged
metal
Global Moderator
Hero Member
*****
Offline Offline

Posts: 2018

Thank You
-Given: 514
-Receive: 477


Top Topic Starter


« Reply #6 on: August 15, 2014, 12:34:05 12:34 »

was he an academic, you mean?
Logged

nothing to say..
solutions
Hero Member
*****
Offline Offline

Posts: 1446

Thank You
-Given: 591
-Receive: 851



« Reply #7 on: August 17, 2014, 12:43:43 12:43 »

I know quite a few knowledge hungry scientist who after climbing the academic ladder fast reaching a level of perfection died unexpectedly at a very young age. Perhaps so many sleepless nights and a chronic neglect of bodily health are the reasons to blame. Some of us might be going through a similar experience. So beware. In any case, RIP Darrel.

I had one 6 years ago, younger than he was.

Unless you get an invasive angiogram, you won't know.

"Live every day like you think it will be your last. One day you will be right"

Posted on: August 17, 2014, 12:36:45 12:36 - Automerged

Here is another example if you program with pic basic pro. Written by Darrel Taylor, who unfortunately passed away recently.

http://www.darreltaylor.com/DT_Analog/

Closed to new registration  Cry

Anyone up to posting a text file with a userid and pw to his site?

thanks
Logged
Checksum8
V.I.P
Junior Member
*****
Offline Offline

Posts: 71

Thank You
-Given: 45
-Receive: 53


« Reply #8 on: August 17, 2014, 08:29:19 20:29 »


Closed to new registration  Cry

Anyone up to posting a text file with a userid and pw to his site?

thanks

From what I recall his site was never  open to registration. At least to the public. I think it got spammed in the early days, so he just used it as place to link some of his projects.
All his major work, Instant Interrupts, MBAM, Oversampling, Averaging, SPWM, etc
can be found at

 http://www.picbasic.co.uk/forum/forum.php
Logged
metal
Global Moderator
Hero Member
*****
Offline Offline

Posts: 2018

Thank You
-Given: 514
-Receive: 477


Top Topic Starter


« Reply #9 on: August 18, 2014, 08:16:24 08:16 »

seems the 8-bit era has started to fade, even its people do now. most people are moving towards cortex-M0, even the PICs and AVRs will be abandoned some day, the same applies to 8051 architecture which stood for a long time because it was the first option for industrial. cortex-M0 consumes less power than 8/16-bit MCUs and offers better architecture and code portability as well as pricing which is very tempting to betray the 8-bit.

Atmel did it already, they did never invest in 8-bit as microchip did, people thought that microchip will be eating Atmel all the time and many AVR users started to think about PICs because of the peripherals microchip provided, rather Atmel has produced cortex-M0 chips, and also replaced AVRISP mkII and their debugger mkII as well with Atmel-ICE which costs $80. The war now is becoming like which is better, MIPS or ARM, rather than AVR32 and PIC32!

This doesn't apply to Atmel alone, many companies produce cortex-M0 now, which shows that investing in 8-bit MCUs is a waste of time. It seems that technology and people fade at the same time, replaced by new technology and accompanied with new people who adopt it.
Logged

nothing to say..
Cain
Junior Member
**
Offline Offline

Posts: 90

Thank You
-Given: 102
-Receive: 106


« Reply #10 on: August 20, 2014, 10:03:55 10:03 »

It seems that technology and people fade at the same time, replaced by new technology and accompanied with new people who adopt it.

I agree, it's probably a "genarational thing". If I was 14-15 years today why would I start with a 8-bit when 32-bit is available for the same price or even cheaper?

That being said I still think 8-bit PIC/AVR/8051/etc will be around for quite some time.

« Last Edit: August 20, 2014, 10:24:21 10:24 by Cain » Logged
Vineyards
Active Member
***
Offline Offline

Posts: 130

Thank You
-Given: 36
-Receive: 25


« Reply #11 on: August 20, 2014, 01:36:07 13:36 »

Most of my projects don't even use an 8bit microcontroller up to its limits. Speedwise, I opt for 4 MHz where the limit can be 40-80MHz. I don't have any need for more processing power. I  rarely use all the memory on a PIC18f4620. Mostly, I don't even get close to the limit. In my line of business using a 32bit processor is like trying to kill a mosquito with a howitzer. As a result, I expect that, 8 bit processors will be around for at least another 10 years if not more.
Logged
Qiaozhi
Newbie
*
Offline Offline

Posts: 7

Thank You
-Given: 4
-Receive: 0


« Reply #12 on: January 27, 2016, 10:51:19 22:51 »

I  rarely use all the memory on a PIC18f4620. Mostly, I don't even get close to the limit.
In one project I managed to consume 91% of the ROM in a PIC18F4520, and 21% of RAM.
There were a lot of heavy calculations, and several options in the menu system.

I've also destroyed several of these devices in the R&D process over the years. Sad
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC