| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| Aprendiz 
 
 
 Joined: 18 Jan 2013
 Posts: 2
 
 
 
			    
 
 | 
			
				| Digital Voltmeter Code |  
				|  Posted: Fri Jan 18, 2013 12:10 pm |   |  
				| 
 |  
				| Hi, i am newbie to programming and trying to make a digital voltmeter using 16f676. But my code is not working. Here is the code: 
  	  | Code: |  	  | #include <16F676.h>
 #device adc=10
 #fuses INTRC_IO,NOWDT,PUT,NOPROTECT,BROWNOUT,NOMCLR
 #use delay (clock=20M) // 20MHz clock
 
 #rom  0x3ff={0x3444}
 
 #byte PORTA = 0x05
 #byte PORTC = 0x07
 #byte TRISA = 0x85
 #byte TRISC = 0x87
 
 #define SPORTA PORTA
 #define SPORTC PORTC
 
 #define  TICKS_BETWEEN_INTERRUPTS      5000 //5000
 #define  INTERRUPT_OVERHEAD            35
 #define  TMR1RESET (0xFFFF-(TICKS_BETWEEN_INTERRUPTS-INTERRUPT_OVERHEAD))
 
 const char SegCode[11] = {0x40,0x57,0x22,0x06,0x15,0x0C,0x08,0x56,0x00,0x04,0xFF};
 //                       0    1    2    3    4    5    6    7    8    9
 const char Column[3]   = {0x02,0x01,0x04};
 static char Segment[3] = {0x7f,0x7f,0x7f};
 static unsigned char ColCount=0x00;
 
 void CPU_SETUP(void);
 void Display(void);
 void HTO7S(unsigned int32 Num);
 
 byte i;
 unsigned int32 result;
 
 #INT_TIMER1
 void Timer1(void)
 {
 set_timer1(TMR1RESET);
 Display();
 }
 
 void main()
 {
 unsigned char i;
 
 CPU_SETUP();
 
 while(true)
 {
 result=0;
 for (i=0;i<20;i++)
 {
 set_adc_channel(3);
 delay_ms(1);
 result=result+read_adc();
 }
 //result = 0x3fe;
 HTO7S(result/20);
 delay_ms(200);
 }
 
 }
 
 void CPU_SETUP()
 {
 
 setup_comparator(NC_NC_NC_NC);   // not use comparator module
 setup_adc_ports( sAN3 | VSS_VDD);
 setup_adc(ADC_CLOCK_DIV_64);
 TRISA=0b00011000;
 PORTA=0x27;
 TRISC=0b00000000;
 PORTC=0x37;
 
 
 setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
 set_timer1(TMR1RESET);
 enable_interrupts(GLOBAL);
 enable_interrupts(INT_TIMER1);
 }
 
 //-------------------------------------
 // Display routine
 //-------------------------------------
 void Display()
 {
 PORTA = 0b00100111;     // off all digits column and Segment G
 PORTC = 0b00111111;   // off segment a-f
 delay_cycles(2);
 
 
 if (ColCount>=3)
 ColCount=0;
 
 SPORTC = Segment[ColCount];
 SPORTA = ((Segment[ColCount] & 0b01000000)>>1) | (Column[ColCount]^0x07);
 ColCount++;
 }
 
 //--------------------------------------
 // Convet HEX 2 byte to 7-Segment code
 //--------------------------------------
 void HTO7S(unsigned int32 Num)
 {
 
 unsigned int32 res;
 
 
 Segment[0]=SegCode[30*Num/10230];
 if (Segment[0]==0x40)
 Segment[0]=0xFF;
 
 res = 30*Num%10230;
 Segment[1]=SegCode[10*res/10230];
 res=10*res%10230;
 Segment[2]=SegCode[10*res/10230];
 }
 
 | 
 
 Here is schematic:
 http://sdrv.ms/WMOvXI
 
 Can somebody help me with this little project??
 |  | 
	
		|  | 
	
		| PCM programmer 
 
 
 Joined: 06 Sep 2003
 Posts: 21708
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Jan 18, 2013 1:10 pm |   |  
				| 
 |  
				|  	  | Quote: |  	  | #use delay (clock=20M) // 20MHz clock 
 #define  TICKS_BETWEEN_INTERRUPTS      5000   // 5000
 #define  INTERRUPT_OVERHEAD            35
 #define  TMR1RESET (0xFFFF-(TICKS_BETWEEN_INTERRUPTS-INTERRUPT_OVERHEAD))
 
 #INT_TIMER1
 void Timer1(void)
 {
 set_timer1(TMR1RESET);
 Display();
 }
 
 setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
 
 | 
 Read the comments in this thread about the display update rate:
 http://www.ccsinfo.com/forum/viewtopic.php?t=49461
 |  | 
	
		|  | 
	
		| temtronic 
 
 
 Joined: 01 Jul 2010
 Posts: 9587
 Location: Greensville,Ontario
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Jan 18, 2013 1:13 pm |   |  
				| 
 |  
				| 'it doesn't work'.. Ok WHAT doesn't work?
 The PIC..
 The A2D system..
 The display system..
 Wrong readings..
 What are 'right' readings..
 
 I can see lots 'wrong' with the code don't know about the hardware though...
 for starters the 676 has a 4MHz internal oscillator, NOT 20MHz...
 
 You should at least start off with the '1Hz Blinking LED' program and confirm it works...
 Then get that LED display to count from say 000 to 999 at sat a 1Hz rate.
 Then add the A2D code
 
 hth
 jay
 |  | 
	
		|  | 
	
		| gpsmikey 
 
 
 Joined: 16 Nov 2010
 Posts: 588
 Location: Kirkland, WA
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Jan 18, 2013 1:17 pm |   |  
				| 
 |  
				| You need to do a better job of defining "not working" - is is displaying anything?  Are you sure the processor is running ? What is happening ?  One thing I always do in my projects is to put a little code in an ISR that sets a spare pin high at the start of the ISR then low before exiting - that gives me a test point I can view with a scope to verify it is a)running at all and b) the clocks are set right since I know when the pulses should be happening. 
 mikey
 _________________
 mikey
 -- you can't have too many gadgets or too much disk space !
 old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19962
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Jan 18, 2013 1:43 pm |   |  
				| 
 |  
				| Another obvious comment: 
  	  | Code: |  	  | #fuses INTRC_IO,NOWDT,PUT,NOPROTECT,BROWNOUT,NOMCLR
 #use delay (clock=20M) // 20MHz clock
 
 | 
 
 What clock rates does the internal oscillator support?.....
 
 Best Wishes
 |  | 
	
		|  | 
	
		| ronaldoklais 
 
 
 Joined: 18 Dec 2012
 Posts: 13
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Jan 18, 2013 7:25 pm |   |  
				| 
 |  
				| BR? |  | 
	
		|  | 
	
		| Aprendiz 
 
 
 Joined: 18 Jan 2013
 Posts: 2
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun Jan 20, 2013 7:04 am |   |  
				| 
 |  
				| It is not showing readings at all. When i increase pot resistance, it displays 888 and when i decrease resistance it does not show any reading.. I think i should start with led blinking codes...
 Can someone recommend some books/tutorials regarding pic programming. Thanks.
 |  | 
	
		|  | 
	
		| Mike Walne 
 
 
 Joined: 19 Feb 2004
 Posts: 1785
 Location: Boston Spa UK
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun Jan 20, 2013 7:41 am |   |  
				| 
 |  
				|  	  | Aprendiz wrote: |  	  | It is not showing readings at all. When i increase pot resistance, it displays 888 and when i decrease resistance it does not show any reading.. I think i should start with led blinking codes...
 Can someone recommend some books/tutorials regarding pic programming. Thanks.
 | 
 Yes I agree.
 
 1) There are many 'C for dummies' books . Pick the one you like.
 2) Do more reading,  CCS manual, PIC data sheets, forum guidelines.
 3) Start from simple beginnings.
 4) Try out some of the CCS examples, LED blink, UART, LCD, ISR etc.
 5) Learn to drive the peripherals for yourself, one at a time.
 6) Write your own code.
 7) Learn to debug.
 8) If (when) you get stuck, ask for help on a specific issue.
 
 Mike
 |  | 
	
		|  | 
	
		| gpsmikey 
 
 
 Joined: 16 Nov 2010
 Posts: 588
 Location: Kirkland, WA
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun Jan 20, 2013 11:01 am |   |  
				| 
 |  
				| There are a couple of things missing from your schematic - where is the power supply to the PIC ?  It is also not obvious how you are programming the PIC - if you are using something like the PICKIT2, that can be configured to act as a "uart tool" and your code can write to it with status/debug information (although it uses a software uart, so you need to disable interrupts during the print to avoid scrambling what you are sending). See Ch 7 of this guide http://ww1.microchip.com/downloads/en/devicedoc/51553e.pdf 
 mikey
 _________________
 mikey
 -- you can't have too many gadgets or too much disk space !
 old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
 |  | 
	
		|  | 
	
		|  |