| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| vas57 
 
 
 Joined: 31 Jan 2022
 Posts: 29
 
 
 
			    
 
 | 
			
				| 18f14k22 DAC |  
				|  Posted: Fri Aug 11, 2023 11:36 pm |   |  
				| 
 |  
				| Hello, I want to make an analog reference for a speed regulator, it's the first time I work with the DAC from 18F14K22, I used the routine below for the test, but I can't find the signal generated by it at the RA0 pin. What is wrong? Thanks for any answer.
 
  	  | Code: |  	  | #include <18F14K22.h>
 #use delay(clock=4M)
 void main(void)
 {
 int8 i;
 setup_dac(DAC_OUTPUT | DAC_VSS_VDD);
 while(1)
 {
 for(i=0; i< 128; i++)
 dac_write(i);
 delay_us(10);
 }
 }
 
 | 
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19967
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sat Aug 12, 2023 1:17 am |   |  
				| 
 |  
				| First thing, learn to use the code buttons to post code. I have modified your posting with these.
 
 Then, how are you detecting this?.
 You have read section 21.6 in the data sheet, and looked at the example
 figure 21-2?.
 You cannot 'drive' anything from this pin, without a buffer....
 
 Ideally you should also turn off the digital input on the pin, otherwise
 the PIC can draw extra power (set_analog_pins). The data sheet tells
 you this must be done for analog inputs, but it should also be done for
 an analog output.
 |  | 
	
		|  | 
	
		| vas57 
 
 
 Joined: 31 Jan 2022
 Posts: 29
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sat Aug 12, 2023 4:01 am |   |  
				| 
 |  
				| Thank you Ttelmah for reply. I modified the DAC test but I still have no signal on the analog output.
 
 
  	  | Code: |  	  | #include <18F14K22.h> #use delay(internal = 4MHZ)
 
 void main(void)
 {
 #byte VREFCON0 = 0xFBA
 #byte VREFCON1 = 0xFBB
 VREFCON1 = 0xA8;//VREFCON1 = 0xC8;
 VREFCON0 = 0xA0;
 set_tris_a( 0xFC );
 int8 i=0;
 setup_dac(DAC_OUTPUT | DAC_VSS_VDD);
 
 while(1)
 {
 for(i=0; i< 128; i++)
 dac_write(i);
 delay_us(10);//delay_us(1000);
 }
 
 }
 | 
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19967
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sat Aug 12, 2023 5:32 am |   |  
				| 
 |  
				| I repeat: 
 "Then, how are you detecting this?.
 You have read section 21.6 in the data sheet, and looked at the example
 figure 21-2?.
 You cannot 'drive' anything from this pin, without a buffer.... "
 
 You can only detect this with a high impedance detector like a scope.
 If you have this connected to a load, you will get almost nothing.
 
 Also get rid of your changes. All you want is:
 set_analog_pins(PIN_A0);
 
 To tell the chip this is an analog pin.
 You have set it as an output pin, so it cannot now work at all, This overrides
 any analog use.
 
 You also need to delay after each output, not just once:
 
  	  | Code: |  	  | while(1)
 {
 for(i=0; i< 128; i++)
 {
 dac_write(i);
 delay_us(10);//delay_us(1000);
 }
 }
 
 | 
 |  | 
	
		|  | 
	
		| vas57 
 
 
 Joined: 31 Jan 2022
 Posts: 29
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sat Aug 12, 2023 6:38 am |   |  
				| 
 |  
				| Thanks Ttelmah, I'm using a Hantek DSO4254C oscilloscope, with 1Mohm/20pF probes.
 I insert the modification with A0 analog channel and it behaves the same, it does not send a sawtooth signal at the output.
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19967
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sat Aug 12, 2023 11:09 am |   |  
				| 
 |  
				| and you have the delay as shown?. |  | 
	
		|  | 
	
		| temtronic 
 
 
 Joined: 01 Jul 2010
 Posts: 9589
 Location: Greensville,Ontario
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sat Aug 12, 2023 5:35 pm |   |  
				| 
 |  
				| curious.. downloaded the datasheet... 20 pin device, seems DAC1OUT pin is RA0 which is ALSO used as PGD ( programming pin ).
 
 If this is correct, have you disconnected your programmer from the PIC ??
 |  | 
	
		|  | 
	
		| vas57 
 
 
 Joined: 31 Jan 2022
 Posts: 29
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sat Aug 12, 2023 9:22 pm |   |  
				| 
 |  
				|  	  | Ttelmah wrote: |  	  | and you have the delay as shown?. | 
 Yes. Now delay is 1000.
 |  | 
	
		|  | 
	
		| vas57 
 
 
 Joined: 31 Jan 2022
 Posts: 29
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sat Aug 12, 2023 9:23 pm |   |  
				| 
 |  
				|  	  | temtronic wrote: |  	  | curious.. downloaded the datasheet... 20 pin device, seems DAC1OUT pin is RA0 which is ALSO used as PGD ( programming pin ).
 
 If this is correct, have you disconnected your programmer from the PIC ??
 | 
 My PICKIT3 sure it is disconnected when I test the program.
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19967
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun Aug 13, 2023 1:21 am |   |  
				| 
 |  
				| OK. Let's step through a few things: 
 What compiler version?.
 Have you verified the chip is running?. Toggle another pin in the loop,
 and verify this works.
 The DAC only gives 32 output steps, not 128.
 
 So:
 
  	  | Code: |  	  | #include <18F14K22.h>
 #FUSES NOMCLR, NOWDT, NOLVP
 #use delay(internal = 4MHZ)
 
 void main(void)
 {
 int8 i;
 setup_dac(DAC_OUTPUT | DAC_VSS_VDD);
 set_analog_pins(PIN_A0);
 delay_ms(10); //ensure supply is stable before starting
 
 while(TRUE) //using TRUE avoids error message
 {
 for(i=0; i< 32; i++)
 {
 dac_write(i);
 delay_ms(10);
 }
 output_toggle(PIN_A1); //check chip is running
 }
 }
 
 | 
 
 You say the programmer is disconnected when testing. Is everything
 disconnected from the pin?.
 I suspect you have something else wrong. Perhaps the MCLR pin is not
 being pulled up when you disconnect your programmer?. I have turned
 this off in the above.
 
 This runs on a chip here.
 |  | 
	
		|  | 
	
		| vas57 
 
 
 Joined: 31 Jan 2022
 Posts: 29
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun Aug 13, 2023 4:15 am |   |  
				| 
 |  
				| Thanks Ttelmah for reply. CCS Compiler version is 5.066
 I run the program under MPLAB v. 8.92 (CCS Compiler) and test it step by step. The program is in the while(1) loop: it does dac_write(i) 32 times(steps), continue with delay_ms(10) once step and then starts from the beginning with 32xdac_write(i),... But on the RA0, pin 19, it does not output anything.
 I will also test with an LED on pin A1.
 |  | 
	
		|  | 
	
		| vas57 
 
 
 Joined: 31 Jan 2022
 Posts: 29
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun Aug 13, 2023 4:21 am |   |  
				| 
 |  
				| My PICKIT3 is totally disconnected when 18f14k22 is running, I remove the cable from the ICSP connector! MCLR is 1 when uC is running. |  | 
	
		|  | 
	
		| vas57 
 
 
 Joined: 31 Jan 2022
 Posts: 29
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun Aug 13, 2023 4:33 am |   |  
				| 
 |  
				| I tested with led: led toggle with 650ms period, so the program is in the while(1) loop. |  | 
	
		|  | 
	
		| temtronic 
 
 
 Joined: 01 Jul 2010
 Posts: 9589
 Location: Greensville,Ontario
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun Aug 13, 2023 4:58 am |   |  
				| 
 |  
				| curious about this problem... questions I have .
 
 1) what opamp have you connected to the RA0 pin?
 
 2) dump the listing and confirm that the proper register bits are set (enable and output )
 |  | 
	
		|  | 
	
		| vas57 
 
 
 Joined: 31 Jan 2022
 Posts: 29
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun Aug 13, 2023 5:34 am |   |  
				| 
 |  
				|  	  | temtronic wrote: |  	  | 2) dump the listing and confirm that the proper register bits are set (enable and output )
 | 
 
 Thank you temtronic.
 I don't use the op-amp as a buffer, I visualize the signal on RA0 with the oscilloscope above.  To point 2) I don't know how to answer, what should I do?
 |  | 
	
		|  | 
	
		|  |