update 2
I want to make a program that can play sound (that can say red, green and blue) on my pic 18f4550 there is a speaker connected to the picdem, that part works fine, I wrote the following program with microchip version 6.83 with the C compiler.
I need to retrieve the bits value of a .wav file, when I say red (this has a bit pattern).
My right question, how can a get the bit value of my .wav file.
void main (void)
{
TRISD = 0x00; // PORTD als uitgang
TRISB = 0b00110000; // RB4 en RB5 als ingang
TRISA = 0x00; // RA output
RCONbits.IPEN = 0; // interrupts
INTCONbits.GIE = 1;
INTCONbits.RBIE = 1;
while(1)
{
_asm sleep _endasm
}
}
#pragma interrupt ISR
void ISR (void)
{
int red[] = {bit values off sound red???};
int blue[] = {bit values off sound green???};
int green[] = {bit values off sound blue???};
if (INTCONbits.RBIF==1)
{
if(PORTBbits.RB5==0) // S3 pressed?
{
int i = 0;
int b;
do {
LATAbits.LATA2 = rood[i];
LATDbits.LATD1 ^= 1;
b = 0;
//do-while voor de frequentie (1500 is de freq)
do {
b++;
}while(b <= 2000);
i++;
}while(rood[i] <= 50);
//LATDbits.LATD1 ^= 1; // D2 togglen
}
}
INTCONbits.RBIF = 0;
}
Answer to original question:
The "frequency of red, represent in bits" you asked for is 480 to 405 THz, so in bits that is 111100000 to 110010101. But you also said "its defined in nanometers", so that is 630 to 740nm which is 1001110110 to 1011100100 in binary. If you want to drive a loudspeaker with a waveform, you need either a sample audio waveform or a speech synthesis chip. In either case, the binary values I have given you have nothing to do with the sound of the word "red" which would need many more bits to represent any audible sound (eg a minimum of 0.5 second at 16 kbps PCM would be 8 kbp = 1 kbyte).
Another way representing a colour in binary is the RGB system where red would be 0xFF0000, which is 111111110000000000000000 in binary.
Answer to question on how to get bits in wav file
First we have to determine where the wav file is stored. It could be stored as a const array in the PIC, or in an external memory device where you have to read it over some kind of serial or parallel bus. Since the PIC18F4550 has 32k bytes of flash, if your program is fairly small there could be enough room left for the 3 wav files.
Then we have to determine how the hardware is going to play the sound. You seem to be attempting to send a byte value out of bit port by shifting it. But to get this right we need to know more about the hardware, because you cannot connect a speaker to a bit port and expect sound out (not without further processing of a PCM signal anyway - are you attempting a 1 bit DAC design? If so, there are further notes here but this is quite ambitious).
Apart from that, the bit values in wav file, would be obtained by obtaining each sample (8 bit?) in the file and shifting a mask value of 0x01 round with a bitwise AND operation to determine which bits were set.
There is absolutely no correlation between the binary values that make up the encoding for a some letters of English, and the PCM values that would make up a sampled version of the sound of someone saying the encoded word.
If you want to play back the sound of someone saying "red", you will first have to sample it and store the resulting bits somewhere, then feed them to your output at an appropriate bitrate. The sampled sound is likely to be a lot larger than just the ASCII representation of "red" (which is 24 bits).
There are integrated chips that contain such samples and that actually can generate sound given an ASCII-encoded word, one example is this one. Unless you have such a chip connected to your MCU, your question is not making a lot of sense.
Based on what you've said in your comments, it sounds like you want to get a stream of bits from the ASCII representation of a string like "red"
. You can do that by looping over each character of the string, and then looping over each bit position:
const char *str = "I hate programming";
// Loop over each character
for (i = 0; i < strlen(str); i++)
{
// Loop over each bit
for (j = 0; j < 8; j++)
{
// Extract bit #j of character i, using bit-shift and mask
bit = (str[i] >> j) & 1;
// bit will be 0 or 1, so do something with it here
}
}
If you want the bits in each character in the opposite order, then just reverse the order of the inner (j
) loop.
Note that as @unwind says in his answer, this has nothing to do with actually generating an audio waveform for spoken words.