Skip to content

Instantly share code, notes, and snippets.

@tiagohm tiagohm/MCP41XXX.c
Created Mar 1, 2018

Embed
What would you like to do?
Potenciometro Digital de 256 posições e 1 canal.
/*
MCP41XXX - Potenciometro Digital de 256 posições e 1 canal.
Autor: Tiago Melo
Blog: Microcontrolandos
Compilador: MikroC PRO PIC
Bibliotecas: Soft_SPI
*/
//Pinos do MCP41XXX.
sbit SoftSpi_SDI at RB1_bit;
sbit SoftSpi_SDO at RB1_bit;
sbit SoftSpi_CLK at RB0_bit;
sbit SoftSpi_SDI_Direction at TRISB1_bit;
sbit SoftSpi_SDO_Direction at TRISB1_bit;
sbit SoftSpi_CLK_Direction at TRISB0_bit;
sbit MCP41XXX_CS at RB2_bit;
sbit MCP41XXX_CS_Direction at TRISB2_bit;
void MCP41XXX_Init() {
//Inicia o pino CS como saída e valor inicial = 1.
MCP41XXX_CS_Direction = 0;
MCP41XXX_CS = 1;
}
void MCP41XXX_Write(char valor) {
MCP41XXX_CS = 0;
//Define o valor do potenciômetro 0.
Soft_SPI_Write(0b00010001);
Soft_SPI_Write(valor);
MCP41XXX_CS = 1;
}
void main() {
char valor = 0;
//Inicia a comunicação SPI.
Soft_SPI_Init();
//Inicializa o MCP41XXX.
MCP41XXX_Init();
while(1) {
//Define o valor do potenciômetro.
MCP41XXX_Write(valor++);
//Faz nada por 100ms.
Delay_ms(100);
}
}
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.