Libstock prefers package manager

Package Manager

We strongly encourage users to use Package manager for sharing their code on Libstock website, because it boosts your efficiency and leaves the end user with no room for error. [more info]

Project Request

Return
tarik ahitos
tarik1985

posted on 2014/05/23 05:15:31 PM CEST

Measurement

ADC_PIC18F2550

helle,

i need code that can help to understand the ADC with choice of Frequency oscillator and how to chose the coeeficient of Tad by two way pleas:
1-wethout using function Read_ADC()
2-with using function ADC_read
i have tried the two method with different oscillator frequency and using datasheet but it's didn't work and i had problem with Tad and didn't work on simulation
thank you very much

USER Comments

Robinson Velasquez
Cagiva125

posted on 2014/07/14 07:13:43 AM CEST

Hola,

En este libro hay ejemplos de las funciones ADC y la configuración del oscilador (en la parte de USB).


https://www.google.com.co/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CBwQFjAA&url=http%3A%2F%2Fwww.mikroe.com%2Fdownload%2Feng%2Fdocuments%2Fpublications%2Fother-books%2Flibro_simulacion_mikroc.pdf&ei=wH7DU66QHIbfsAT4oYH4CQ&usg=AFQjCNH0P-n93V_PxmLiyL_Lsz6Iv3ac8Q&sig2=dXp8xXsTiWbd6EJOqTTGQg&bvm=bv.70810081,d.cWc




Este código lo editado puede servir:



/*
* Project name:
Virtual COM Port Demo
* Description
Example showing usage USB CDC device that functions as a generic virtual
COM port. The example echoes data sent via USART terminal.
* Test configuration:
MCU: P18F2550
Oscillator: 4MHZ HS-PLL, 48.000MHz
Ext. Modules: None.
SW: mikroC PRO for PIC
http://www.mikroe.com/mikroc/pic/
Notes: Example uses CDC Driver included in Windows. An .inf file
is supplied with the example in order to match the driver
with this device (user must guide driver installation
wizard to the VCPDriver folder).
La memoria RAM del 18F2550 es de 1Kb.
Va desde la dirección 0x400 hasta 0x7FF.


*/

#include <stdint.h>

// Buffer of 64 bytes
unsigned char write_buffer[64] absolute 0x500;
unsigned char read_buffer[64] absolute 0x700;

unsigned int Secuencia=0, Cont=0;

unsigned int Muestra=0;

long Salida=0;
unsigned char Dato_Salida=0;
int Dato=0, Decenas=0, Unidades=0;

// USB interrupt service routine
void interrupt(){
// Call library interrupt handler routine
USBDev_IntHandler();
}

void USBDev_CDCParamsChanged(){

}

extern const uint8_t _USB_CDC_BULK_EP_IN; // Data interface IN endpoint
extern const uint8_t _USB_CDC_BULK_EP_OUT; // Data interface OUT endpoint
uint8_t dataReceived = 0;
uint16_t dataReceivedSize;
// CONFIGURACIONES DE LA COMUNICACION SERIAL:
const uint32_t dwDTERate=9600; // Baud Rate
const uint8_t bCharFormat=1; // Stop Bits
const uint8_t bParityType=0; // Paridad
const uint8_t bDataBits=8; // Bits de info

void USBDev_CDCDataReceived(uint16_t size){
dataReceived = 1;
dataReceivedSize = size;
}




void Capturar_ADC()
{ /*Read_Buffer[0]=0; Read_Buffer[1]=0; Read_Buffer[2]=0; Read_Buffer[3]=0;
Read_Buffer[4]=0; Read_Buffer[5]=0;*/

Salida=ADC_Read(0);
LongIntToStrWithZeros(Salida,write_Buffer);

// Send packet
// USBDev_CDCSendData(write_buffer, 12);

delay_us(10);

}


void main(void)
{
//ADCON1 |= 0x0F; // Configure all ports with analog function as digital

CMCON |= 7; // Disable comparators

ADCON1 = 0b001110; // Solo RA0 es analógico, los otros 11 son digitales.
ADCON1.VCFG0 = 0; // 0 VREF + = VDD 1 VREF + = AN3
ADCON1.VCFG1 = 0; // 0 VREF - = VSS 1 VREF - = AN2


//ADCON2 PARA SELECCIONAR EL TIEMPO DE ADQUISICIÓN:
// SEGÚN DATASHEET PARA 12TAD:
ADCON2.ACQT2=1; ADCON2.ACQT1=0; ADCON2.ACQT0=1;

// ADCON 2 PARA SELECCIONAR TAD TIEMPO DE ADQUISICIÓN POR BIT:
// CON ESTA CONFIGURACIÓN EL TAD ES DE 64 TOSC PARA FOSC DE 48MHz
// SI Fosc = 48MHz Tosc es de 1,33333uS
// 64 Tosc = 16uS CADA TAD.
ADCON2.ADCS0= 0; ADCON2.ADCS1= 1; ADCON2.ADCS2= 1;
// EN EL DATASHEET CONVERSION CLOCK 110 EQUIVALE A FOSC/64

//YA CONFIGURADO EL ADCON 2... SON 12TAD CADA UNO A 16uS
// LA CONVERSIÓN EN TOTAL SE DEMORA 192uS


TRISA.F0=1; // RA0 ES ENTRADA ANALÓGICA
TRISA.F1=0; // RA1 ES SALIDA DIGITAL LED ROJO
TRISC.F6=0; // SALIDA RC6 LED VERDE
PORTA.F1=1; // PARA INDICAR CON UN LED ROJO QUE YA ESTÁ CONECTADO.
TRISB=0;
PORTB=0;

// Initialize CDC Class
USBDev_CDCInit();

// Initialize USB device module
USBDev_Init();

// Enable USB device interrupt
IPEN_bit = 1;
USBIP_bit = 1;
USBIE_bit = 1;
GIEH_bit = 1;

// Wait until device is configured (enumeration is successfully finished)
while(USBDev_GetDeviceState() != _USB_DEV_STATE_CONFIGURED)
;

// Set receive buffer where received data is stored
USBDev_CDCSetReceiveBuffer(read_buffer);

USBDev_CDCSetLineCoding(dwDTERate,bCharFormat,bParityType,bDataBits);

Delay_ms(1000);
PORTC.F6=1; // PRENDER EL LED VERDE


Inicio:

// Infinite loop
while(1)
{

PORTB=0b0;
Delay_ms(5);
PORTB=0b1;
Delay_ms(5);

/*if(dataReceived == 1)
{
dataReceived = 0;
Capturar_ADC();
// Prepare for receive:
USBDev_CDCSetReceiveBuffer(read_buffer);

if (dataReceivedSize==1)
{
write_buffer[0]=48;
write_buffer[1]=Read_buffer[0];

if (Read_buffer[0]==48) PORTB=0b0000; // 00
if (Read_buffer[0]==49) PORTB=0b0001; // 01
if (Read_buffer[0]==50) PORTB=0b0010; // 02
if (Read_buffer[0]==51) PORTB=0b0011; // 03
if (Read_buffer[0]==52) PORTB=0b0100; // 04
if (Read_buffer[0]==53) PORTB=0b0101; // 05
if (Read_buffer[0]==54) PORTB=0b0110; // 06
if (Read_buffer[0]==55) PORTB=0b0111; // 07
if (Read_buffer[0]==56) PORTB=0b1000; // 08
if (Read_buffer[0]==57) PORTB=0b1001; // 09

Read_buffer[0]=48; Read_buffer[1]=48; // Reiniciar el Dato Recibido

USBDev_CDCSendData(write_buffer, 12);

goto Inicio;
}

//USBDev_CDCSetReceiveBuffer(read_buffer);

if (dataReceivedSize==2)
{ // m 01 1m envia 1
// m 11 1m envia 11
// M 00 1m
//m 976 m
//00000001024// Esto es lo que lee LabView.
//00001201024// Esto lee si envía un 12.

write_buffer[0]=Read_buffer[0];
write_buffer[1]=Read_buffer[1];

if (Read_buffer[1]==48) PORTB=0b1010; // 10
if (Read_buffer[1]==49) PORTB=0b1011; // 11
if (Read_buffer[1]==50) PORTB=0b1100; // 12
if (Read_buffer[1]==51) PORTB=0b1101; // 13
if (Read_buffer[1]==52) PORTB=0b1110; // 14
if (Read_buffer[1]==53) PORTB=0b1111; // 15

Read_buffer[0]=48; Read_buffer[1]=48; // Reiniciar el Dato Recibido

USBDev_CDCSendData(write_buffer, 12);

goto Inicio;
}

}


*/
}
}







El programa completo con la configuración del oscilador lo puede descargar de este Link:

https://dl.dropboxusercontent.com/u/46164500/USB%20CDC%20FUNCIONANDO%20V7%20Modificando%20TAD.rar

  • Rating:
5
No Abuse Reported

Do you want to report abuse comment ID: 1201 in "ADC_PIC18F2550" request.