PGA2311 code - Page 2 - diyAudio
Go Back   Home > Forums > Source & Line > Analog Line Level

Analog Line Level Preamplifiers , Passive Pre-amps, Crossovers, etc.

Please consider donating to help us continue to serve you.

Ads on/off / Custom Title / More PMs / More album space / Advanced printing & mass image saving
Reply
 
Thread Tools Search this Thread
Old 27th January 2013, 05:15 AM   #11
diyAudio Member
 
Join Date: Jun 2011
Sorry, please accept my apologies, you'll have to ignore some of the stuff I've written above, it's incorrect and stupid. I should have looked at the datasheet before commenting.

I had a look at the ATTiny2313 datasheet, I've got a bit clearer idea of what's going on.

I see that all the PCINT0->7 interrupts all use the same vector.

The one thing I did see in your code is that PORTB, 0 is not configured as as input. I read that the inputs respond even when the pins are not enabled as inputs, but this is only in the case when the value is changed by the program outputting to the pin, this permits the creation of a software interrupt. This however should not affect the volume control, only the mute control.

I can see that they use this

Code:
    PCMSK |= (1<<PCINT0);
syntax all over the place, once you get used to it it's easy enough to deal with.

Here's how I think it could be made to work without interrupts.

Code:
#include "avr/io.h"
#include "compat/deprecated.h"
#define F_CPU 1000000UL
#include "avr/delay.h"

int main(void);
void PGA_volume(unsigned char volume_r, unsigned char volume_l);
void volup(void);
void voldn(void);
void mymute(void);

unsigned char left, right, b0det, d2det, d3det;

int main(void){
	DDRB = 0b11111110;                        	        // Port B 7->1 as output, 0 as input
	DDRD = 0b00000000;                        	        // Port D as input
	sbi (PORTB, 7);                                     // enable ZCEN
	sbi (PORTB, 6);                                     // Disable chip select
	cbi (PORTB, 5);                                     // Clear bit data
	cbi (PORTB, 2);                                     // Clear bit clock
	cbi (PORTB, 1);                                     // disable mute
	left = 20;
	right = 20;
	b0det = 0;
	d2det = 0;
	d3det = 0;
	
	PGA_volume(right, left);

	while(1)
	{
		if (PINB & (1<<PB0)){
			b0det=1;
		}
		else{
			b0det=2;
		}
		if (PIND & (1<<PD2)){
			d2det=1;
		}
		else{
			d2det=2;
		}
		if (PIND & (1<<PD3)){
			d3det=1;
		}
		else{
			d3det=2;
		}
		if (b0det==1){
	  	    if (!(PINB & (1<<PB0))){
			    b0det=2;
			    mymute();
		    }
	    }
	    if (b0det==2){
	        if (PINB & (1<<PB0)){
		        b0det=1;
		        mymute();
	        }
        }
        if (d2det==1){
			if (!(PIND & (1<<PD2))){
				d2det=2;
				volup();
			}
        }
        if (d2det==2){
            if (PIND & (1<<PD2)){
                d2det=1;
                volup();            
            }
        }
        if (d3det==1){
            if (!(PIND & (1<<PD3))){
                d3det=2;
                voldn();            
            }
        }
        if (d3det==2){
            if (PIND & (1<<PD3)){
                d3det=1;
                voldn();            
            }
        }
    }		
}

void PGA_volume(unsigned char volume_r, unsigned char volume_l)
{
	unsigned char i;
	unsigned char send_r, send_l;

	cbi (PORTB, 6);                                 // Enable chip select
	for(i = 0 ; i < 8 ; i++)                         // Send volume right
	{
		send_r = volume_r & 0b10000000; 
		if (send_r){
			sbi (PORTB, 5);                 // Set bit data
			_delay_us(100);
			sbi (PORTB, 2);                 // Set bit clock
			_delay_us(50);
			cbi (PORTB, 2);                 // Clear bit clock
			_delay_us(50);
		}
                else{
			cbi (PORTB, 5);
			_delay_us(100);
			sbi (PORTB, 2);
			_delay_us(50);
			cbi (PORTB, 2);
			_delay_us(50);
		}
		volume_r = volume_r << 1;
	}
	for(i = 0 ; i < 8 ; i++)                        // Send volume left
	{
		send_l = volume_l & 0b10000000;
		if (send_l){
			sbi (PORTB, 5);
			_delay_us(100);
			sbi (PORTB, 2);
			_delay_us(50);
			cbi (PORTB, 2);
			_delay_us(50);
		}
                else{
			cbi (PORTB, 5);
			_delay_us(100);
			sbi (PORTB, 2);
			_delay_us(50);
			cbi (PORTB, 2);
			_delay_us(50);
		}
		volume_l = volume_l << 1;
	}
	sbi (PORTB, 6);                                 // Disable chip select
	cbi (PORTB, 5);                                 // Clear bit data
	cbi (PORTB, 2);                                 // Clear bit clock
	return;
}

void volup(void)                                          // Increment volume
{		
	// Disable mute
	sbi (PORTB,1);
	if(left < 125)
	{
		left = left + 1;
		right = right + 1;
	}
	PGA_volume(right,left);
}


void voldn(void)                                          // Decrement volume
{
	// Disable mute
	sbi (PORTB,1);
	if(left > 0)
	{
	left = left - 1;
	right = right - 1;
	}	
	PGA_volume(right,left);
}


void mymute(void)                                         // Toggle mute
{
    if (PINB & (1<<PB1)){
        cbi (PORTB,1);
    }
    else{
	sbi (PORTB,1);
    }
}
One of the things I could suggest for debugging is to lift one of the pins you are using for inputs and driving it directly either by pulling it up or down. You could tie it to ground with a resistor and then toggle it using a flying wire connected to Vcc.

Sorry again about the comments I made earlier, I really was out of line.

Let us know how you get on, and I will try to help you solve this problem. There are very few bugs that you cannot discover with a bit of ingenuity in thinking of a test.
  Reply With Quote
Old 27th January 2013, 09:40 AM   #12
child1 is offline child1  Denmark
diyAudio Member
 
Join Date: Jan 2013
@counter culture - no need to apologize. I realize that I sometimes mix the syntaxes together and that it can look messy! The reason for this is that some code is copied from another source and some of it I have written myself. I study electrical engineering and this is the way I was tought to do it Thats all I can say... I appreciate all your inputs. Afterall it led me to simplify the code and now

IT WORKS!!!!!

I deleted the whole thing and started from scratch myself. The only thing not working is the mute. But that is caused by the Rentron TINY IR remote decoder which only outputs 0.67V on the assigned mute pin when triggered, so I suspect it to be faulty!

Thanks you guys for all your help...!!
  Reply With Quote
Old 27th January 2013, 02:30 PM   #13
rsavas is offline rsavas  Canada
diyAudio Member
 
Join Date: Aug 2012
Location: Ontario
Well done @counter culture & @child1, cheers!!
It would be great to have a code vault on this site to place this code & others, like the stuff I was going to release, for others to find easily.
  Reply With Quote

Reply


Hide this!Advertise here!
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PGA2311 Evaluation KatieandDad Analog Line Level 7 3rd February 2013 05:58 PM
PGA2311 daisy-chaining yallo Chip Amps 2 23rd March 2012 05:39 PM
Problem witht [code]blah[/code] ? elseif Forum Problems 1 17th August 2008 01:30 AM
How to use PGA2311 in a gainclone? Spasticteapot Chip Amps 12 10th March 2007 10:42 PM
PGA2311 control Hisatugo Digital Source 0 15th June 2005 11:07 PM


New To Site? Need Help?

All times are GMT. The time now is 05:51 PM.


vBulletin Optimisation provided by vB Optimise (Pro) - vBulletin Mods & Addons Copyright © 2014 DragonByte Technologies Ltd.
Copyright 1999-2014 diyAudio

Content Relevant URLs by vBSEO 3.3.2