asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
Simple MCP42xx driver |
Posted: Mon Aug 27, 2012 4:09 pm |
|
|
For MCP4251 and others in the MCP42x1 family DUAL pots
with 0-257 step control - these are used in an audio appliance
and the SPI Lines are up to 6 feet long - with terminators at the far end.
The delay functions are needed ONLY for use with an 18F46K22 running at Fosc 64 Mhz.
There are other features I neither need nor care about - this merely sets the pots accurately and to any step required.
Code: |
// #define PLS pin_a0
// #define PCK pin_a1
// #define PDI pin_a2
// write pot simple - DUAL channel MCP 42xx effective
// work is the value from 0 to 257 to send to the pot
// whichpot=0 is A pot = 1 is B pot
void setMCP42pot(int1 whichpot, unsigned int16 work){
unsigned int8 i;
#bit pob = work.15
// note: even tho a 10 bit field ,
// bit 9 is always zero as 257 is max arg for the part
work &=0b0000000111111111; // clear any value outside 9 bit range
if (!whichpot) work |=0b0001000000000000;
output_high(PCK);
delay_cycles(4);
output_low(pls);
delay_cycles(4);
output_low(PCK);
for ( i=0; i<16; i++){
delay_cycles(2);
if(pob) output_high(PDI );
else output_low(PDI);
output_high(PCK);
work <<=1;
output_low(PCK);
}
delay_cycles(2);
output_high(pls);
output_low(PCK);
}
|
|
|