Last active
February 27, 2018 11:59
-
-
Save tiagohm/38e5d1d80ff1de822400c0b3d805e9ac to your computer and use it in GitHub Desktop.
Conversor Analógico-Digital de 12 bits.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
MCP4725 - Conversor Analógico-Digital de 12 bits. | |
Autor: Tiago Melo | |
Blog: Microcontrolandos | |
Compilador: MikroC PRO PIC | |
Bibliotecas: Soft_I2C, Conersions, C_String | |
*/ | |
//Pinos do MCP4725. | |
sbit Soft_I2C_Scl at RB0_bit; | |
sbit Soft_I2C_Sda at RB1_bit; | |
sbit Soft_I2C_Scl_Direction at TRISB0_bit; | |
sbit Soft_I2C_Sda_Direction at TRISB1_bit; | |
#define MCP4725_ADDR(addr, rw) (0xC0 | (addr << 1) | rw) | |
//Comandos | |
#define FAST_MODE_CMD 0x00 | |
#define WRITE_DAC_CMD 0x40 | |
#define WRITE_DAC_EEPROM_CMD 0x60 | |
void MCP4725_Write(unsigned valor) { | |
//Inicia a transmissão. | |
Soft_I2C_Start(); | |
//Envia o endereço do dispositivo. | |
Soft_I2C_Write(MCP4725_ADDR(0, 0)); | |
//Envia os 12 bits do valor de saída. | |
Soft_I2C_Write(FAST_MODE_CMD | (((char*)&valor)[1] & 0x0F)); | |
Soft_I2C_Write(((char*)&valor)[0]); | |
//Encerra a transmissão. | |
Soft_I2C_Stop(); | |
} | |
//Pinos dos botões. | |
sbit Botao_Incrementar at RA0_bit; | |
sbit Botao_Decrementar at RA1_bit; | |
void main() { | |
char flags = 0; | |
unsigned valor = 0x800; //50% ou 2.5V. | |
Soft_I2C_Init(); | |
//Define os pinos dos botões como entrada digital. | |
TRISA = 0x3; | |
ADCON1 = 0x7; | |
//Envia o valor inicial de saída. | |
MCP4725_Write(valor); | |
while(1) { | |
//Lembrando que estamos utilizando botões com resistores de pull-up. | |
//Isto é, o estado normal(não pressionado) é = 1. | |
//Queremos que faça algo após ser pressionado e solto. | |
//Botão foi pressionado após estar não pressionado. | |
if(!flags.B0 && !Botao_Incrementar) { | |
//salva o estado do botão. | |
flags.B0 = 1; | |
} | |
if(!flags.B1 && !Botao_Decrementar) { | |
//salva o estado do botão. | |
flags.B1 = 1; | |
} | |
//O botão foi solto após estar pressionado. | |
if(flags.B0 && Botao_Incrementar) { | |
//salva o estado do botão. | |
flags.B0 = 0; | |
//Incrementa o valor de saída. | |
valor++; | |
//Estourou o valor máximo. Resetar. | |
if(valor > 0xFFF) valor = 0; | |
//Escreve o valor de saída. | |
MCP4725_Write(valor); | |
} | |
if(flags.B1 && Botao_Decrementar) { | |
//salva o estado do botão. | |
flags.B1 = 0; | |
//Decrementa o valor de saída. | |
valor--; | |
//Estourou o valor mínimo. | |
//Ao decrementar o valor 0, valor será igual à 0xFFFF. | |
if(valor > 0xFFF) valor = 0xFFF; | |
//Escreve o valor de saída. | |
MCP4725_Write(valor); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment