dslocum
Newbie level 5
- Joined
- Feb 5, 2016
- Messages
- 10
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 90
void Vol_Output(unsigned short channel, unsigned short valueVOL) {
unsigned short temp;
// Send address Byte
Vol_Write_Byte(channel);
// Send volume value Byte
Vol_Write_Byte(valueVOL);
}
void main(void) {
unsigned short i;
// init stuff here...
while(1) {
for(i=0; i<127; i++) { //
Vol_Output(0, i);
Delay_ms(10);
}
for(i=127; i>0; i--) {
Vol_Output(0, i);
Delay_ms(10);
}
}
}
void Vol_Output(unsigned short channel, unsigned short valueVOL) {
unsigned short temp;
// Send address Byte
Vol_Write_Byte(channel);
// added this to 'invert' the input volume
temp = 127 - valueVOL;
// Send volume value Byte
Vol_Write_Byte(temp);
}
void main(void) {
unsigned short i;
// init stuff here...
while(1) {
for(i=0; i<127; i++) { //
Vol_Output(0, i);
Delay_ms(10);
}
for(i=127; i>0; i--) {
Vol_Output(0, i);
Delay_ms(10);
}
}
}
"Doesn't work" means you are seeing what exactly?
if(temp > 127)
temp=127;
if(temp < 0)
temp = 0;
Just reverse the two for loops but can you make out the difference? What you are trying to do?
// added this to 'invert' the input volume
temp = 127 - valueVOL;
// Send volume value Byte
Vol_Write_Byte(temp);
Vol_Write_Byte(127 - valueVOL)
"unwanted type casting"? Only if the compiler ignores elementary C rules. I've seen strange compiler bugs, in so far I won't deny the possibility.
I would either inspect the assembly listing or trace code operation with a hardware debugger to see what happens.
Code is correct. You get (as you already wrote) correct values during stepping, so the error is not in the code.
Are you sure the delays provide enough time for hardware to adjust?
Are you sure there is no need to add any termination byte?
Is the Vol_Output function working propertly?
void Vol_Output(unsigned short channel, unsigned short valueVOL) {
unsigned short temp;
// Send address Byte
Vol_Write_Byte(channel);
// added this to 'invert' the input volume
temp = 127 - valueVOL;
if(temp > 127)
set a led1 ON //this led should never be lit
if((temp < 64){//the led2 should toggle and could be watched for pulses or the mcu pin
//could be measured with a DVM
set led2 ON
} else {
set led2 OFF
}
// Send volume value Byte
Vol_Write_Byte(temp);
}
There are a few simple debugging techniques to use, so you will be sure that no casting errors occur.
One of them is to use a snippet of code in your function:
Code:void Vol_Output(unsigned short channel, unsigned short valueVOL) { unsigned short temp; // Send address Byte Vol_Write_Byte(channel); // added this to 'invert' the input volume temp = 127 - valueVOL; if(temp > 127) set a led1 ON //this led should never be lit if((temp < 64){//the led2 should toggle and could be watched for pulses or the mcu pin //could be measured with a DVM set led2 ON } else { set led2 OFF } // Send volume value Byte Vol_Write_Byte(temp); }
// WORKS
temp = valueVOL;
if(temp > 64) {
LATB0_bit = 1;
}
else {
LATB0_bit = 0;
}
// WORKS
temp = valueVOL;
if(temp > 127) {
LATB0_bit = 1;
}
else {
LATB0_bit = 0;
}
// NOT WORKING, LED DOES NOT LIGHT (no value over 127
temp = 127 - valueVOL;
if(temp > 127) {
LATB0_bit = 1;
}
else {
LATB0_bit = 0;
}
// NOT WORKING, LED LIGHTS CONSTANTLY
temp = 127 - valueVOL;
if(temp > 126) {
LATB0_bit = 1;
}
else {
LATB0_bit = 0;
}
// NOT WORKING, LED LIGHTS CONSTANTLY
temp = 127 - valueVOL;
if(temp == 127) {
LATB0_bit = 1;
}
else {
LATB0_bit = 0;
}
See if you can use Arduino's map() function.
void Vol_Output(unsigned short channel, unsigned short valueVOL) {
int temp;
// Send address Byte
Vol_Write_Byte(channel);
// added this to 'invert' the input volume
temp = 127 - ((int)valueVOL);
// Send volume value Byte
Vol_Write_Byte((unsigned short)temp);
}
void Vol_Output(unsigned short channel, unsigned short valueVOL) {
unsigned short temp;
// Send address Byte
Vol_Write_Byte(channel);
// added this to 'invert' the input volume
temp = ((unsigned short)127) - valueVOL;
// Send volume value Byte
Vol_Write_Byte(temp);
}
void main() {
unsigned short z;
unsigned short x;
ANSB0_bit = 0; // led bit
LATB0_bit = 0;
TRISB0_bit = 0;
while(1) {
for(z=0; z<127; z++) {
// this works
x = z;
// this also works
// x = 127 - z;
if(x > 64) {
LATB0_bit = 1;
}
else {
LATB0_bit = 0;
}
Delay_ms(10);
}
}
}
void foo(unsigned short valueVOL) {
unsigned short x;
x = valueVOL;
if(x > 64) {
LATB0_bit = 1;
}
else {
LATB0_bit = 0;
}
}
void main() {
unsigned short z;
ANSB0_bit = 0; // led bit
LATB0_bit = 0;
TRISB0_bit = 0;
while(1) {
for(z=0; z<127; z++) {
foo(z);
Delay_ms(10);
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?