Skip to content

Instantly share code, notes, and snippets.

@tiagohm tiagohm/MCP3201.c
Created Mar 9, 2018

Embed
What would you like to do?
Conversor Analógico/Digital de 12-bits.
/*
MCP3201 - Conversor Analógico/Digital de 12-bits.
Autor: Tiago Melo
Blog: Microcontrolandos
Compilador: MikroC PRO PIC
Bibliotecas: LCD, Lcd_Constants, Conversions, C_String
*/
//Pinos do MCP3201.
sbit MCP3201_Clock at RB0_bit;
sbit MCP3201_Data at RB1_bit;
sbit MCP3201_CS at RB2_bit;
sbit MCP3201_Clock_Direction at TRISB0_bit;
sbit MCP3201_Data_Direction at TRISB1_bit;
sbit MCP3201_CS_Direction at TRISB2_bit;
//Pinos do LCD.
sbit LCD_RS at RC0_bit;
sbit LCD_EN at RC1_bit;
sbit LCD_D4 at RC2_bit;
sbit LCD_D5 at RC3_bit;
sbit LCD_D6 at RC4_bit;
sbit LCD_D7 at RC5_bit;
sbit LCD_RS_Direction at TRISC0_bit;
sbit LCD_EN_Direction at TRISC1_bit;
sbit LCD_D4_Direction at TRISC2_bit;
sbit LCD_D5_Direction at TRISC3_bit;
sbit LCD_D6_Direction at TRISC4_bit;
sbit LCD_D7_Direction at TRISC5_bit;
void MCP3201_Init() {
MCP3201_CS_Direction = 0;
MCP3201_Clock_Direction = 0;
MCP3201_Data_Direction = 1;
MCP3201_CS = 1;
MCP3201_Clock = 0;
}
unsigned int MCP3201_Read() {
char i;
unsigned value;
//Inicia a conversão.
MCP3201_Clock = 1;
MCP3201_CS = 0;
//Lê 15 bits. Os 3 primeiros serão ignorados.
for(i = 0; i < 15; i++) {
value <<= 1;
MCP3201_Clock = 1;
Delay_us(1);
MCP3201_Clock = 0;
Delay_us(1);
//Lê o valor após um pulso de descida do clock.
value |= MCP3201_Data;
}
MCP3201_Clock = 0;
MCP3201_CS = 1;
return (value & 0x0FFF);
}
float MCP3201_Convert(unsigned int raw, float vref) {
return raw * vref / 4096.0;
}
void main() {
char texto[12];
//Inicializa o MCP3201.
MCP3201_Init();
//Inicializa o LCD.
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
while(1) {
//Lê o valor.
unsigned valor = MCP3201_Read();
//Converte o valor lido em um valor de tensão com 5V como referência.
float tensao = MCP3201_Convert(valor, 5);
//Converte em texto.
FloatToStr(tensao, texto);
//Escreve no LCD.
Lcd_Out(2, 1, texto);
Lcd_Chr_CP('V');
//Faz nada por 1s.
Delay_ms(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.