View previous topic :: View next topic |
Author |
Message |
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
void ds1307_display() |
Posted: Fri Nov 02, 2018 10:45 am |
|
|
Quote: | char time[] = "TIME: : : ";
char calendar[] = "DATE: / /20 ";
unsigned int8 second, second10, minute, minute10,
hour, hour10, date, date10, month, month10,
year, year10, day;
void ds1307_display(){
second10 = (second & 0x70) >> 4;
second = second & 0x0F;
minute10 = (minute & 0x70) >> 4;
minute = minute & 0x0F;
hour10 = (hour & 0x30) >> 4;
hour = hour & 0x0F;
date10 = (date & 0x30) >> 4;
date = date & 0x0F;
| I am trying to understand the language But I am failing
1. What is happening by the statement in bold part?
2. In case of second 0x70 & 0x0F is used but for minute why 0x30 & 0x0F?
3. When the program will execute for the first time then what will be the value of second and second10 after first time execution? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Fri Nov 02, 2018 11:08 am |
|
|
This converts a value into separate bytes.
So if you had a number '0x59' say, this is converted into separate bytes '5' and '9'. This is what the functions you highlight do.
The 'seconds10' variable would receive the '5'. The three bits of the source value in the top nibble ( & 0x70, would give 0x50), shifted right, to become '5'.
Then seconds is loaded back with just the low digit.
You are wrong in '2'. It uses 0x70, & 0xF again for the minutes. It uses 0x30, and 0xF, for the _hours_. Reason is simply that hours can't go over '24', and the chip returns the top two bits as 'undefined', so these must not be used. While with the seconds and minutes only the top bit should not be used.
You would not call the function twice, without re-loading the values.
The reason is because the clock chip stores the data in BCD. This has the decimal digits in the nibbles of the byte returned.
So 59 seconds, is stored as 0x59, decimal 89, so if you just printed it you would get the wrong value. You could use the hex output format in printf, to do a similar conversion. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Sat Nov 03, 2018 3:37 am |
|
|
As a further comment, the driver you are trying to use, appears to be the one from:
<https://simple-circuit.com/pic16f877a-ds1307-alarm-ccs-c/>
Now honestly, the ds1307 driver in the code library, is really simpler code:
<http://www.ccsinfo.com/forum/viewtopic.php?t=23255&highlight=ds1307> |
|
|
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
|
Posted: Sat Nov 03, 2018 3:52 am |
|
|
Thanks to you Mr. Ttelmah. I am grateful to you. |
|
|
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
|
Posted: Sat Nov 03, 2018 8:55 am |
|
|
Quote: | void ds1307_write(unsigned int8 address, data_)
{
i2c_start(); // Start I2C
i2c_write(0xD0); // DS1307 address
i2c_write(address); // Send register address
i2c_write(data_); // Write data to the selected register
i2c_stop(); // Stop I2C
} |
1. Is address of DS1307 is (0xD0)?
2. Send register address; which register address? Is it address of a DS1307 resister where data is to be written?
3. Write data to the selected register; which data to be written in selected resister? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Sat Nov 03, 2018 9:10 am |
|
|
quick answers...
1) Yes, the DS1307 has a fixed address of 0xD0 for writes to it, 0xD1 for reading from it. This is in the DS1307 datasheet, a MUST read by the way ! Some I2C devices have programmable device address, typically called A0,A1, A2 on the pinout.
2) which DS1307 register address is decided by the 'address' passed to the function
3) what data is passed to the DS1307 is decided by the 'data_' argument of the function
example...
...
address = 0x10;
data_ = 0x12;
ds1307_write(unsigned int8 address, data_)
...
should write 0x12 to the DS1307 register ox10
Naturally it's up to you to code the correct data so that valid data is transferred to the DS1307.
Jay |
|
|
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
|
Posted: Sun Nov 04, 2018 3:24 am |
|
|
Thanks Mr. temtronic!
One more:
SET_TRIS_B(0);= Set all pin of PORTB as output.
SET_TRIS_B(0x0F);=?
What does "SET_TRIS_B(0x0F);" say? |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Nov 04, 2018 3:59 am |
|
|
I'd prefer that you work these things out for yourself.
Do the following help?
SET_TRIS_B(0); is the same as SET_TRIS_B(0b00000000);
SET_TRIS_B(0x0F); is the same as SET_TRIS_B(0b00001111);
Mike |
|
|
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
|
Posted: Sun Nov 04, 2018 4:21 am |
|
|
Mike Walne wrote: | SET_TRIS_B(0b00001111); | Does it mean that set 4 pins of PORTB as output pins and other 4 pins as input pin? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Sun Nov 04, 2018 5:39 am |
|
|
This is basic CCS C and if you press F11 while your project is open, the manual will open up ! Probably 90% of what you need to know is in there AND shown in the hundreds of examples CCS supplies, in the examples folder.
I strongly suggest you read the PIC datasheet as well as several of the application notes that are on Microchip's website.
I understand English is not everyone's 1st language and some of it is confusing.
Pics only have 30-40 instructions and if you can get a grasp of HOW they work at a 'low level', using assembler, then coding in a high level language like C (or BASIC) should be fairly easy. What complicates a lot of code is the lack of line by line comments. Also while PICs are similar, not all PICS have all internal peripherals. After 2+ decades of PICking, I've settled on the 18F46K22 for 99% of the projects. While 'overkill' for most, I've yet to run out of I/O pins or memory. Having 2 HW UARTs is a BIG benefit and it'll run full speed at 3 or 5 volts.
and yes... setting a PIC DDR bit to a '1' does make that pin and 'Input'
very easy to remember, '1' looks like an 'I', '0' looks like an 'O' !!
Jay |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Nov 04, 2018 5:42 am |
|
|
Try it, see what happens.
You can simulate with MPLAB or (preferably) the real thing.
A test should also tell you which four are inputs and outputs.
Mike
Mr T. got in as I typed!
I was hoping the binary form would make it obvious what was happening.
As others will point out, you should not need to use the TRIS instructions with CCS as their I/O functions take care of them for you. |
|
|
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
|
Posted: Wed Nov 07, 2018 9:06 am |
|
|
Quote: | output_x(value) - Outputs an entire byte to the port. | Now in the following code " output_b(0);" indicate which entire byte should put to the port B?
Code: | void main(){
setup_oscillator(OSC_8MHZ); // Set internal oscillator to 8MHz
setup_adc_ports(NO_ANALOGS); // Configure AN pins as digital
port_b_pullups(TRUE);
output_b(0);
set_tris_b(0x0F);
lcd_init(); // Initialize LCD module
lcd_putc('\f'); |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Wed Nov 07, 2018 9:25 am |
|
|
We can't tell.
Depends entirely on what _you_ have connected to PortB, and what signals the attached devices need to see.... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Wed Nov 07, 2018 9:27 am |
|
|
output_b(0);
It puts 0b00000000 to Port B
Whatever 8 bit value inside the (...) will be sent to Port B.
IE: output_b( 0b11001111); will set bits 7,6,3,2,1,0 of Port B .
BTW, if you press F11 when your project is open, the CCS manual will open for you.
Jay |
|
|
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
|
Posted: Wed Nov 07, 2018 11:55 am |
|
|
temtronic wrote: | BTW, if you press F11 when your project is open, the CCS manual will open for you. | As you instructed I start to search the manual. But there is also not clearly explained.
form the manual-
Quote: | rtc_write(rtc_time_t datetime) -Writes the date and time to the RTCC module. |
In the following code:
minute/hour/month/date/year=time. but date=? 1/2/4/5/6=date?
Quote: | while(TRUE){
if(input(PIN_B3) == 0)
year++;
if(year > 99)
year = 0;
lcd_gotoxy(9, 2); // Go to column 9 row 2
printf(lcd_putc,"%02u", year);
if(input(PIN_B2) == 0){
// Convert decimal to BCD
minute = ((minute/10) << 4) + (minute % 10);
hour = ((hour/10) << 4) + (hour % 10);
date = ((date/10) << 4) + (date % 10);
month = ((month/10) << 4) + (month % 10);
year = ((year/10) << 4) + (year % 10);
// End conversion
ds1307_write(1, minute);
ds1307_write(2, hour);
ds1307_write(4, date);
ds1307_write(5, month);
ds1307_write(6, year);
ds1307_write(0, 0); //Reset seconds and start oscillator
delay_ms(200);
break;
} |
|
|
|
|