View previous topic :: View next topic |
Author |
Message |
sradhika1998
Joined: 21 Jun 2019 Posts: 12
|
Help this newbie in using digital potentiometer with LM386 |
Posted: Sun Jun 30, 2019 2:06 pm |
|
|
Hello,
I am newbie at CCS programming. I am designing speaker with digital potentiometer. The amplifier is LM386 and digital potentiometer is MCP4131 (100k). I am using PIC18F4520 microcontroller at 8Mhz frequency.
I have code for it and checked it in Proteus. In starting I get the result but later it's distroted. I am even not sure about connection and code.
Here is the link of my project, https://drive.google.com/file/d/1QHNxqeWKzp2rSDr7pHt8O0fzUNK9sQoW/view?usp=sharing
Please guide me that where I am wrong and where I can improve and how.
Awaiting response.
Thanks[/url] |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Jun 30, 2019 2:45 pm |
|
|
Sorry, can't help, your link did not work, (tried it twice).
Read the posts at the head of the general discussion.
The advice there is to dispense with ISIS/Proteus.
It simply does not work with CCS & PICs.
You will waste more time that it's worth by using it.
Go straight to real hardware, and get real results.
Mike |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Sun Jun 30, 2019 3:20 pm |
|
|
I tried as well, but can not download any of your files.
'Distorted' sound could be wrong gain for the LM386, wrong components, wrong power supply.
I suggest eliminating the PIC portion, take the LINE OUT of a PC that you know plays music, and get the LM386 section working...
THEN..
reconnect the PIC and see what happens.
Also redo your 'links' and get them to work, we really can't help much until we can see your program and schematics.
Jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jun 30, 2019 3:51 pm |
|
|
I was able to download it. It consists of 30+ files.
Here is his program:
Code: | #define SSD1306_RST PIN_D4
#include <18F4520.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8MHz)
#use I2C(MASTER, I2C1, FAST = 400000, stream = SSD1306_STREAM) // Initialize I2C
// Include SSD1306 OLED driver source code
#include <SSD1306OLED.c>
#define CS_PIN PIN_B4 //Chip Select
#define CLK_PIN PIN_B5 //Serial Clock
#define DI_PIN PIN_B6 //Serial Data In
#define Up PIN_A0
#define Down PIN_A1
#define Select PIN_A2
// End specification for pins
void DigPot();
void initDigPot();
unsigned short t=0;
unsigned Mask=0x7000|Mask;
void initDigPot(int InitialValue)
//----------------------------------------------
{
setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_16);
output_high(CS_PIN);
}
void DigPot(int Mask)
//----------------------------------------------
//used for MCP41xxx Microchip digital pot
// 0x00 = wiper at PB0, 0xFF wiper at PA0
{
output_low(CS_PIN);
delay_us(1);
spi_write(Mask);
delay_us(1);
for (t=0; t<16; t++){
delay_cycles(2);
if((Mask&0x8000) == 0) output_low(DI_PIN);
else output_high(DI_PIN);
output_high(CLK_PIN);
Delay_us(5);
output_low(CLK_PIN);
Mask = Mask << 1;
}
output_high(CS_PIN);
}
void main()
{
while(1)
{
digpot(1024);
output_high(CS_PIN);
Delay_us(5);
}
return;
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jun 30, 2019 4:14 pm |
|
|
The MCP4131 data sheet says it uses SPI mode 00 or 11 (i.e., mode 0 or 3).
Your code is using SPI mode 2. That's wrong.
See this chart by ckielstra to see the SPI mode:
http://www.ccsinfo.com/forum/viewtopic.php?t=52800&start=8
2nd bug:
You have a routine named initDigPot(), but you don't call it.
It's just sitting there. You need to call it once, near the start of main(). |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Sun Jun 30, 2019 5:28 pm |
|
|
possible bug..
unsigned Mask=0x7000|Mask;
Is 'Mask' a 16bit or 8 bit or 32 bit variable ?
Usually you'd say
unsigned int16 Mask=0x7000|Mask;
to TELL the compiler what size to make the variable.
You shouldn't rely upon 'defaults' ! They were created by another programmer, no necessarily correct for your program. Even defaults between PIC series can differ.
Jay |
|
|
sradhika1998
Joined: 21 Jun 2019 Posts: 12
|
|
Posted: Sun Jun 30, 2019 10:15 pm |
|
|
PCM programmer wrote: | The MCP4131 data sheet says it uses SPI mode 00 or 11 (i.e., mode 0 or 3).
Your code is using SPI mode 2. That's wrong.
See this chart by ckielstra to see the SPI mode:
http://www.ccsinfo.com/forum/viewtopic.php?t=52800&start=8
2nd bug:
You have a routine named initDigPot(), but you don't call it.
It's just sitting there. You need to call it once, near the start of main(). |
Thank, I am making changes as per you said. Have you checked the proteus file? Is the circuit is right? |
|
|
sradhika1998
Joined: 21 Jun 2019 Posts: 12
|
|
Posted: Sun Jun 30, 2019 10:33 pm |
|
|
temtronic wrote: | I tried as well, but can not download any of your files.
'Distorted' sound could be wrong gain for the LM386, wrong components, wrong power supply.
I suggest eliminating the PIC portion, take the LINE OUT of a PC that you know plays music, and get the LM386 section working...
THEN..
reconnect the PIC and see what happens.
Also redo your 'links' and get them to work, we really can't help much until we can see your program and schematics.
Jay |
Hello, I will check it soon. But I am not sure about code and circuit whether its right or wrong. Also the connection of audio to the digital potentiometer to the wiper A is right or wrong.
Here is the code which I made some changes according to people suggested:
Code: | #define SSD1306_RST PIN_D4
#include <18F4520.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8MHz)
#use I2C(MASTER, I2C1, FAST = 400000, stream = SSD1306_STREAM) // Initialize I2C
// Include SSD1306 OLED driver source code
#include <SSD1306OLED.c>
#define CS_PIN PIN_B4 //Chip Select
#define CLK_PIN PIN_B5 //Serial Clock
#define DI_PIN PIN_B6 //Serial Data In
#define Up PIN_A0
#define Down PIN_A1
#define Select PIN_A2
// End specification for pins
void DigPot();
void initializeDigPot();
unsigned short t=0;
unsigned int16 Mask=0x7000|Mask;
void initializeDigPot(int InitialValue)
//----------------------------------------------
{
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);
output_high(CS_PIN);
}
void DigPot(int Mask)
//----------------------------------------------
//used for MCP41xxx Microchip digital pot
// 0x00 = wiper at PB0, 0xFF wiper at PA0
{
output_low(CS_PIN);
delay_us(1);
spi_write(Mask);
delay_us(1);
for (t=0; t<16; t++){
delay_cycles(2);
if((Mask&0x8000) == 0) output_low(DI_PIN);
else output_high(DI_PIN);
output_high(CLK_PIN);
Delay_us(5);
output_low(CLK_PIN);
Mask = Mask << 1;
}
output_high(CS_PIN);
}
initializeDigPot();
void main()
{
while(1)
{
digpot(1024);
output_high(CS_PIN);
Delay_us(5);
}
return;
} |
Is this code is right for digital potentiometer? |
|
|
sradhika1998
Joined: 21 Jun 2019 Posts: 12
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 01, 2019 12:48 am |
|
|
I told you to select either SPI mode 0 or 3. So you put in mode 2.
I told you to call initializeDigPot() near the start of main(). Instead you
stick it out in space, not in main().
I think you need to learn the C language before you go any farther. |
|
|
akash
Joined: 22 Jul 2019 Posts: 1
|
|
Posted: Mon Jul 22, 2019 5:17 am |
|
|
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Mon Jul 22, 2019 7:21 am |
|
|
There is a big issue, which _will_ cause 'distorted sound'.
The pot handles voltages between Vss & Vdd. Not signals that are swinging
each side of ground. To use this type of digital pot, with an audio signal, you
have to DC bias the signal to the midpoint of the supply, before feeding it into
the pot, and then ensure you AC couple into the amplifier stage.
So you have a choice. Bias the voltage like this so it never swings -ve
or use a digipot that is designed for audio applications.
The former is described here:
<https://www.avrfreaks.net/forum/using-digital-pots-audio-signals>
The second would involve a device like the Maxim DS1882.
This are actually 'better' devices for an audio application, since the
control is logarithmic which is much more suitable for such an application.
However it needs dual power supplies. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Mon Jul 22, 2019 8:02 am |
|
|
I was curious, so I downloaded the pot's datasheet. Mr. T is right again.. that pot isn't a good one for analog use.
1st it needs to be bias to 2.7 volts, so the range will be 2.7 to 5, not a lot of room
2nd , it's a linear pot , NOT a log style, so control will be terrible.
seriously I'd look at the part he suggests or use Google to find suitable pot available to you.
also be sure you have CLEAN, stable power to the pot !!
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Mon Jul 22, 2019 8:55 am |
|
|
Yes, even worse than I thought!....
Look at the MCP41010. This is the Microchip device designed for audio
as opposed to just controlling a voltage. Described as 'mixed signal',
supporting both AC and DC inputs. Still limited to a signal between Vss & Vdd,
but does not need any minimum voltage on the analog pins. |
|
|
|